与timeit不一致的性能

时间:2015-10-28 15:34:07

标签: python linux performance cython

为清晰起见编辑:基本上我想知道的是我可以在共享的Linux机器上采取哪些步骤来确保我的Python代码的性能尽可能一致?

我正在尝试使用timeit模块确定两个不同代码段的速度。不幸的是,结果变化很大,我不知道哪个更好。

这里的两段代码是a)用Cython编译的Numpy函数和b)用Cython编译的类C函数。它们基本上生成一堆随机数并将它们分成一个数字(parts)的二进制数。 (没有读取/写入磁盘,所以这不是我的问题。)我在测试期间运行了两次函数,并重复测试200次,从200次测试中选出最佳时间。

我正在运行以测试两个函数的速度的Python代码如下所示:

import timeit

num = 20

repeat = 200

setup = '''import my_cython_code as sc
parts = 100'''

print "pyx: ", min(timeit.repeat('sc.pyx(parts)', 
    number=num, setup=setup, repeat=repeat))
print "c:   ", min(timeit.repeat('sc.c(parts)', 
    number=num, setup=setup, repeat=repeat))
print "pyx: ", min(timeit.repeat('sc.pyx(parts)', 
    number=num, setup=setup, repeat=repeat))
print "c:   ", min(timeit.repeat('sc.c(parts)', 
    number=num, setup=setup, repeat=repeat))
print "pyx: ", min(timeit.repeat('sc.pyx(parts)', 
    number=num, setup=setup, repeat=repeat))
print "c:   ", min(timeit.repeat('sc.c(parts)', 
    number=num, setup=setup, repeat=repeat))
print "pyx: ", min(timeit.repeat('sc.pyx(parts)', 
    number=num, setup=setup, repeat=repeat))
print "c:   ", min(timeit.repeat('sc.c(parts)', 
    number=num, setup=setup, repeat=repeat))
print "pyx: ", min(timeit.repeat('sc.pyx(parts)', 
    number=num, setup=setup, repeat=repeat))

当我运行它时,我得到如下输出:

pyx:  0.00450992584229
c:    0.00507998466492
pyx:  0.00416111946106
c:    0.0036518573761
pyx:  0.00330686569214
c:    0.00355005264282
pyx:  0.00322699546814
c:    0.00346803665161
pyx:  0.00327897071838

为什么会有这么多变化?如何确保性能更加​​一致?我在安装了Scientific Linux 6.6版的共享机器上运行代码。 (我不是管理员,因此无法阻止其他人的流程/等以测试性能。)

注意:我很欣赏在这种情况下,这两个函数看起来几乎同样快,所以我的努力​​可能有点误导。

通常我会使用更大的parts值,在10000-100000的范围内,这使得两个函数都慢得多(~O(n))。如果值越大,这两个函数之间可能会有更显着的差异 - 这就是我感兴趣的内容。在这种情况下,我使用较小的值来进行演示,而不是任何函数。

不幸的是,由于性能不一致,我无法分辨哪个更快,因此这个问题。

遵循高绩效标志的建议:

pyx: min:   0.004488 max:   0.008562 avg:   0.005900 std:   0.001607
c:   min:   0.005030 max:   0.005783 avg:   0.005304 std:   0.000149
pyx: min:   0.004416 max:   0.006408 avg:   0.004995 std:   0.000382
c:   min:   0.004878 max:   0.005824 avg:   0.005357 std:   0.000184
pyx: min:   0.004481 max:   0.005147 avg:   0.004721 std:   0.000130
c:   min:   0.005025 max:   0.005717 avg:   0.005277 std:   0.000145
pyx: min:   0.003409 max:   0.004910 avg:   0.004073 std:   0.000508
c:   min:   0.003712 max:   0.005244 avg:   0.004436 std:   0.000497

平均值大部分时间都比较稳定,但我偶尔会遇到奇怪的表现,如上所述。

num = 1000repeat = 50

pyx: min:   0.022578 max:   0.036049 avg:   0.026740 std:   0.005088
c:   min:   0.024303 max:   0.029202 avg:   0.027013 std:   0.001051
pyx: min:   0.022904 max:   0.025746 avg:   0.024109 std:   0.000616
c:   min:   0.018185 max:   0.026352 avg:   0.020287 std:   0.002845
pyx: min:   0.016775 max:   0.017728 avg:   0.017132 std:   0.000195
c:   min:   0.018132 max:   0.020774 avg:   0.019203 std:   0.000714
pyx: min:   0.016763 max:   0.017589 avg:   0.017049 std:   0.000186
c:   min:   0.017587 max:   0.021777 avg:   0.018697 std:   0.001005

0 个答案:

没有答案
相关问题