题 如何以简洁和全面的方式衡量以下各种功能的性能. 例 考虑数据帧df df = pd.DataFrame({ 'Group': list('QLCKPXNLNTIXAWYMWACA'), 'Value': [29, 52, 71, 51, 45, 76, 68, 60, 92, 95, 99, 27, 77, 54, 39, 23, 84, 37, 99, 8
如何以简洁和全面的方式衡量以下各种功能的性能.
例
考虑数据帧df
df = pd.DataFrame({ 'Group': list('QLCKPXNLNTIXAWYMWACA'), 'Value': [29, 52, 71, 51, 45, 76, 68, 60, 92, 95, 99, 27, 77, 54, 39, 23, 84, 37, 99, 87] })
我想总结由Group中的不同值分组的Value列.我有三种方法可以做到这一点.
import pandas as pd import numpy as np from numba import njit def sum_pd(df): return df.groupby('Group').Value.sum() def sum_fc(df): f, u = pd.factorize(df.Group.values) v = df.Value.values return pd.Series(np.bincount(f, weights=v).astype(int), pd.Index(u, name='Group'), name='Value').sort_index() @njit def wbcnt(b, w, k): bins = np.arange(k) bins = bins * 0 for i in range(len(b)): bins[b[i]] += w[i] return bins def sum_nb(df): b, u = pd.factorize(df.Group.values) w = df.Value.values bins = wbcnt(b, w, u.size) return pd.Series(bins, pd.Index(u, name='Group'), name='Value').sort_index()
它们是一样的吗?
print(sum_pd(df).equals(sum_nb(df))) print(sum_pd(df).equals(sum_fc(df))) True True
它们有多快?
%timeit sum_pd(df) %timeit sum_fc(df) %timeit sum_nb(df) 1000 loops, best of 3: 536 µs per loop 1000 loops, best of 3: 324 µs per loop 1000 loops, best of 3: 300 µs per loop它们可能不会被归类为“简单框架”,因为它们是需要安装的第三方模块,但我经常使用两个框架:
> simple_benchmark
(我是该套餐的作者)
> perfplot
例如,simple_benchmark库允许将函数装饰为基准:
from simple_benchmark import BenchmarkBuilder b = BenchmarkBuilder() import pandas as pd import numpy as np from numba import njit @b.add_function() def sum_pd(df): return df.groupby('Group').Value.sum() @b.add_function() def sum_fc(df): f, u = pd.factorize(df.Group.values) v = df.Value.values return pd.Series(np.bincount(f, weights=v).astype(int), pd.Index(u, name='Group'), name='Value').sort_index() @njit def wbcnt(b, w, k): bins = np.arange(k) bins = bins * 0 for i in range(len(b)): bins[b[i]] += w[i] return bins @b.add_function() def sum_nb(df): b, u = pd.factorize(df.Group.values) w = df.Value.values bins = wbcnt(b, w, u.size) return pd.Series(bins, pd.Index(u, name='Group'), name='Value').sort_index()
还装饰一个产生基准值的函数:
from string import ascii_uppercase def creator(n): # taken from another answer here letters = list(ascii_uppercase) np.random.seed([3,1415]) df = pd.DataFrame(dict( Group=np.random.choice(letters, n), Value=np.random.randint(100, size=n) )) return df @b.add_arguments('Rows in DataFrame') def argument_provider(): for exponent in range(4, 22): size = 2**exponent yield size, creator(size)
然后你需要运行的基准是:
r = b.run()
之后,您可以将结果作为绘图检查(您需要matplotlib库):
r.plot()
如果函数在运行时非常相似,则百分比差异而不是绝对数字可能更重要:
r.plot_difference_percentage(relative_to=sum_nb)
或者将基准测试的时间作为DataFrame(这需要pandas)
r.to_pandas_dataframe()
sum_pd sum_fc sum_nb 16 0.000796 0.000515 0.000502 32 0.000702 0.000453 0.000454 64 0.000702 0.000454 0.000456 128 0.000711 0.000456 0.000458 256 0.000714 0.000461 0.000462 512 0.000728 0.000471 0.000473 1024 0.000746 0.000512 0.000513 2048 0.000825 0.000515 0.000514 4096 0.000902 0.000609 0.000640 8192 0.001056 0.000731 0.000755 16384 0.001381 0.001012 0.000936 32768 0.001885 0.001465 0.001328 65536 0.003404 0.002957 0.002585 131072 0.008076 0.005668 0.005159 262144 0.015532 0.011059 0.010988 524288 0.032517 0.023336 0.018608 1048576 0.055144 0.040367 0.035487 2097152 0.112333 0.080407 0.072154
如果您不喜欢装饰器,您还可以在一次调用中设置所有内容(在这种情况下,您不需要BenchmarkBuilder和add_function / add_arguments装饰器):
from simple_benchmark import benchmark r = benchmark([sum_pd, sum_fc, sum_nb], {2**i: creator(2**i) for i in range(4, 22)}, "Rows in DataFrame")
这里的perfplot提供了一个非常相似的接口(和结果):
import perfplot r = perfplot.bench( setup=creator, kernels=[sum_pd, sum_fc, sum_nb], n_range=[2**k for k in range(4, 22)], xlabel='Rows in DataFrame', ) import matplotlib.pyplot as plt plt.loglog() r.plot()