为批插入计算最佳批大小的库

batch-size-automator的Python项目详细描述


Code QualityPublished

批量自动售货机

batch_size_automator是一个python库,它允许自动检测最佳批处理大小,以优化批处理数据操作(例如,数据库摄取)。在

为什么要用批处理自动机而不是其他库

还有什么图书馆?在

使用批处理自动机

安装此库:

pip install batch_size_automator

关于如何使用BSA的最基本版本。这将照顾到您的批处理大小,并随着时间的推移优化它以获得最大的性能。在

^{pr2}$

设置

本章概述了构造函数参数及其如何影响BSA的行为。在

批次大小

当给定大于0的值时,将禁用自动批处理大小优化,并在调用get_next_batch_size时始终返回此值。在

frombatch_size_automatorimportBatchSizeAutomatorimporttimebsa=BatchSizeAutomator(batch_size=100)whileTrue:batch_size=bsa.get_next_batch_size()# will always return 100 start=time.monotonic()# do your batch operation here using the batch_size variableduration=time.monotonic()-startbsa.insert_batch_time(duration)# will not do anything

数据批次大小

将要处理的数据的最小批处理大小,例如,如果一个数据集总是由5个数据点组成,而这些数据点在下一个独立于返回的批处理大小data_batch_size将被设置为5,这样可以确保返回的批处理大小始终是5的倍数。在

frombatch_size_automatorimportBatchSizeAutomatorimporttimebsa=BatchSizeAutomator(data_batch_size=5)whileTrue:batch_size=bsa.get_next_batch_size()# will always return a multitude of 5 (e.g. 5, 10, 20, 100, 555, ...) start=time.monotonic()# do your batch operation here using the batch_size variableduration=time.monotonic()-startbsa.insert_batch_time(duration)

主动

如果设置为False将禁用自动批处理大小优化的布尔值。在没有改变的情况下,可以减少对其他代码的影响。在

frombatch_size_automatorimportBatchSizeAutomatorimporttimebsa=BatchSizeAutomator(active=False)whileTrue:batch_size=bsa.get_next_batch_size()# will always return 0 (or `batch_size` if set)start=time.monotonic()# do your batch operation here using the batch_size variableduration=time.monotonic()-startbsa.insert_batch_time(duration)# will not do anything

测试尺寸

用于确定经过多少次操作后将比较当前和最佳批次大小,从而计算新的批次大小。不应低于20(以减少异常值的影响),将其设置为大值将增加找到最佳批大小所需的时间。在

frombatch_size_automatorimportBatchSizeAutomatorimporttimebsa=BatchSizeAutomator(test_size=40)whileTrue:batch_size=bsa.get_next_batch_size()start=time.monotonic()# do your batch operation here using the batch_size variableduration=time.monotonic()-startbsa.insert_batch_time(duration)# will trigger recalculation of batch size after 40 iterations

设置尺寸

设置用于在操作之间更改批处理大小的初始步长。如果它小于data_batch_sizedata_batch_size将被用作初始步长

frombatch_size_automatorimportBatchSizeAutomatorimporttimebsa=BatchSizeAutomator(step_size=300)whileTrue:batch_size=bsa.get_next_batch_size()# will return initial batch_size + 300 after the first recalculation start=time.monotonic()# do your batch operation here using the batch_size variableduration=time.monotonic()-startbsa.insert_batch_time(duration)

简化的功能

本章以一个简单的解释说明了BSA是如何工作的。在

模式

BSA包括两种模式:

  • 寻找最佳批量大小
  • 监视

寻找最佳批量大小

  1. BSA计算在最后一个测试周期内每秒插入多少行(test_size插入)
  2. BSA比较当前结果是否优于最佳结果 2.a.如果电流更好,批量大小可根据步长进行调整 2.b.如果电流更差,批量大小将按上次调整的相反方向进行调整,并减小步长
  3. 重复步骤1到2,直到步骤大小低于阈值(这意味着我们经常输入2.b.并且应该已经找到了最佳的批量大小)
  4. 切换到监视模式

监视模式

  1. BSA将测试循环的长度增加到1000个插入
  2. 插入1000次后,BSA计算性能是否变差 2.a.如果性能更差,测试周期长度设置为20,我们切换到寻找最佳批量大小模式 2.b.如果性能相同或更好,则重复步骤1至2

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
Docker&SeleniumJava:无法在Docker容器上运行的chrome浏览器中上载图像/文件   在python中运行java命令   Java垃圾收集器异常行为   java java是否根据底层操作系统执行字节码级优化?   java是否可以休眠自定义查询返回映射而不是列表?   java Spring引导RabbitMQ接收器Jackson反序列化到POJO   apache flex在ActionScript3中创建对象相等“HashMap”作为java HashMap   java如何在Eclipse集成中切换JProfiler启动器   缓存JSP页面结果的java最佳实践?   java集成jaxb绑定文件,使用CXF生成基于WSDL的客户端   java为什么在上传操作结束之前,客户端没有检测到HttpServletResponse的PrintWriter内容?   java在接口内创建类和在类内创建接口有什么用   java向文件写入错误Android Studio   java合并多个RealmList并对结果列表排序?   谷歌API视觉java。lang.NoSuchMethodError   java如何使用逗号分别存储每个值,然后将它们存储到单独的数组中?