timeit支持多种功能,并提供更好的报告

timethese的Python项目详细描述


厌倦了在比较 不止一个功能?时间这些有助于此类微基准测试。 它基本上是在多个函数上运行timeit(或者实际上是重复)和spits 写一份报告。在

一句话:timethis是timeit,用于多个功能,具有更好的报告功能

  • 免费软件:麻省理工学院许可证

安装

pip install timethese

您还可以安装开发中版本:

^{pr2}$

使用

微基准

时间这些有三个步骤:

  1. 定义要比较的函数
  2. 将它们以列表或dict的形式发送给cmp(见下文)
  3. 格式化结果,也就是漂亮的打印

我们来看看:

from timethese import cmpthese, pprint_cmp, timethese

xs = range(10)


# 1. DEFINE FUNCTIONS

def map_hex():
    list(map(hex, xs))


def list_compr_hex():
    list([hex(x) for x in xs])


def map_lambda():
    list(map(lambda x: x + 2, xs))


def map_lambda_fn():
    fn = lambda x: x + 2
    list(map(fn, xs))


def list_compr_nofn():
    list([x + 2 for x in xs])


# 2. FEED THE FUNCTIONS TO CMPTHESE

# AS DICT:

cmp_res_dict = cmpthese(
    10000,
    {
        "map_hex": map_hex,
        "list_compr_hex": list_compr_hex,
        "map_lambda": map_lambda,
        "map_lambda_fn": map_lambda_fn,
        "list_compr_nofn": list_compr_nofn,
    },
    repeat=3,
)


# OR AS LIST:

cmp_res_list = cmpthese(
    10000, [map_hex, list_compr_hex, map_lambda, map_lambda_fn, list_compr_nofn,], repeat=3,
)

# 3. PRETTY PRINT THE RESULTS

print(pprint_cmp(cmp_res_dict))
print(pprint_cmp(cmp_res_list))

如果你运行这个会得到什么?在

根据所提供函数的运行时,速率(单位:1/s)或 显示每次迭代的秒数(s/iter)。在

对于dict来说:

                      Rate  list_compr_nofn  map_hex  map_lambda  map_lambda_fn  list_compr_hex
list_compr_nofn  1385057/s                .      43%         47%            48%             88%
        map_hex   969501/s             -30%        .          3%             4%             31%
     map_lambda   940257/s             -32%      -3%           .             1%             27%
  map_lambda_fn   935508/s             -32%      -4%         -1%              .             27%
 list_compr_hex   738367/s             -47%     -24%        -21%           -21%               .

列出如下内容:

                        Rate  4.list_compr_nofn  0.map_hex  2.map_lambda  3.map_lambda_fn  1.list_compr_hex
4.list_compr_nofn  1360009/s                  .        31%           42%              46%               78%
        0.map_hex  1037581/s               -24%          .            9%              11%               36%
     2.map_lambda   955513/s               -30%        -8%             .               2%               25%
  3.map_lambda_fn   933666/s               -31%       -10%           -2%                .               22%
 1.list_compr_hex   763397/s               -44%       -26%          -20%             -18%                 .

(函数名取自fn.\uu name_u并以列表索引作为前缀。)

定时

timethese还具有timethese功能,供cmp使用 内部。要直接获取计时,可以运行:

from timethese import timethese

xs = range(10)


def map_hex():
    list(map(hex, xs))


def list_compr_hex():
    list([hex(x) for x in xs])


def map_lambda():
    list(map(lambda x: x + 2, xs))


def map_lambda_fn():
    fn = lambda x: x + 2
    list(map(fn, xs))


def list_compr_nofn():
    list([x + 2 for x in xs])


timings_dict = timethese(
    10000,
    {
        "map_hex": map_hex,
        "list_compr_hex": list_compr_hex,
        "map_lambda": map_lambda,
        "map_lambda_fn": map_lambda_fn,
        "list_compr_nofn": list_compr_nofn,
    },
    repeat=3,
)

timings_list = timethese(
    10000,
    [ map_hex, list_compr_hex, map_lambda, map_lambda_fn, list_compr_nofn ],
    repeat=3,
)

# if you want, you can create a pandas df from it

import pandas as pd

timings_df = pd.DataFrame(timings_dict.values())
print(timings_df)

# BEWARE: if you pass a list to timings, you have to skip the .values() call

timings_df = pd.DataFrame(timings_list)
print(timings_df)

带装饰器的定时函数

时间这些还为时间单个函数提供了修饰符:

import time
import timethese

@timethese.print_time
def calculate_something():
    time.sleep(1)

calculate_something()

提供4个装饰工,2个用于普通材料

  • timethese.print_time
  • timethese.log_time(logger, level=logging.INFO)

对于pandas数据帧(它们也打印结果数据帧的形状)为2。 使用df.pipe(...)

  • timethese.log_time_df(logger, level=logging.INFO)
  • timethese.print_time_df

例如,要记录pandas数据帧上管道操作的执行时间,可以编写:

import time
import logging
import timethese
import numpy as np
import pandas as pd

logging.basicConfig(level=logging.DEBUG)

logger = logging.getLogger(__name__)


@timethese.log_time_df(logger, logging.DEBUG)
def sum_by_group(df):
    time.sleep(1)  # introduce some artificial delay
    return df.groupby("A").sum()


df = pd.DataFrame({"A": np.arange(100) % 2, "B": np.random.normal(size=100)})

res = df.pipe(sum_by_group)

有关更好的示例,请参阅源代码中的函数文档。在

发展

要运行所有测试运行:

tox

请注意,要合并来自所有tox环境的覆盖率数据,请执行以下操作:

Windows^{pr 10}$
Other^{pr 11}$

另请参见

这个想法来自Perl的Benchmark.pm,我在过去的日子里经常使用它。在

变更日志

0.0.7(2020-05-31)

  • 改进了文档并修复了错误

0.0.6(2020-05-31)

  • 改进的文档
  • 固定的设置.py安装依赖项以再次通过travis测试

0.0.5(2020-05-31)

  • 添加了更好的文档
  • 现在对函数def doc使用NumPy文档格式
  • 修复了pyproject.toml项目在

0.0.4(2020-05-30)

  • 修复了与python3.5兼容的代码
  • 修正了特拉维斯的东西
  • 为的特定于时间的函数添加了装饰器pandas.DataFrame.pipe论据

0.0.3(2020-05-27)

  • PyPI的第一个版本。在

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

推荐PyPI第三方库


热门话题
java可靠地从Kafka主题获取最后一条(已经生成的)消息   在双精度数据类型的情况下,某些语言的java小数点精度会丢失   java Hibernate条件查询不同对象的不同属性   添加JFreeChart关闭文档时出现java itextpdf异常   数组在java中类名是唯一更改的情况下,我可以使用继承吗?   java javafx应用程序生成但不会运行,没有显示错误   javafx Java逻辑异或(“^”)与逻辑非(“!”)   java我无法构建我的项目。我不知道怎么解决这个问题。请帮帮我   jakarta ee为什么我在使用上下文时会得到一个NameNotFoundException。查找(“java:comp/env/MyBean”)   java Firebase获取值并更改/更新它   java组织。xml。萨克斯。SAXS异常;cvcelt。1:找不到元素“data”的声明   java MongoDB SocketException::连接重置   Hmm格式的java不可解析日期异常SimpleDataFormat   不带XML的java Dozer API日期映射配置   React Native:JAVA\u HOME设置为无效目录:C:\Program Files\JAVA\jdk1。8.0_181   java无法使用JUnitParams运行PowerMock   java ImageIO。阅读(png)失去透明度   java如何禁用jmockit覆盖率检测