Python 在所有系统上的执行时间不一致 [英] Inconsistent Execution Time in Python on all systems

查看:34
本文介绍了Python 在所有系统上的执行时间不一致的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我对 python 感到疯狂的东西......我曾经认为它是只是 Windows,但我错了.我可以拥有完全相同的代码并多次运行它,它的执行时间却大不相同.以下面的测试代码为例:

Something that's been driving me crazy with python... I used to think it was just Windows, but I was wrong. I can have the same exact code and run it multiple times and it executes in wildly different amounts of time. Take the following test code, for example:

import math
def fib(count):
    x = 0
    while x < count:
        a = int(((((1 + math.sqrt(5)) / 2) ** x) - (((1 - math.sqrt(5)) / 2) ** (x))) / math.sqrt(5))
        x+=1

if __name__ == '__main__':  
    import timeit

    t = timeit.Timer("fib(1250)", setup="from __main__ import fib",)
    #print t.timeit(10)
    count = 10000
    results = t.repeat(count, 1)
    min = 0xFFFF
    max = 0
    sum = 0
    for i in results:
        i = i*1000.0
        if i < min: min = i
        if i > max: max = i
        sum+=i

    print "Min {:.3f} | Max {:.3f} | Max/Min {:.3f} | Avg {:.3f}".format(min, max, max/min, sum/count)

基本上,它生成 fibonacii 的前 1250 个元素 10,000 次,并使用 timeit 获取每次运行所需的时间.然后我合并这些时间并找到最小值、最大值、平均值以及最小值和最大值之间的方差(如果你愿意的话,就是差值).

Basically, it generates the first 1250 elements of fibonacii 10,000 times and uses timeit to get the amount of time each run takes. I then coalesce those times and find min, max, average and variance between min and max (the spread, if you will).

结果如下:

Windows: Min 3.071 | Max 8.903 | Max/Min 2.899 | Avg 3.228
Mac OS:  Min 1.531 | Max 3.167 | Max/Min 2.068 | Avg 1.621
Ubuntu:  Min 1.242 | Max 10.090 | Max/Min 8.123 | Avg 1.349

所以,Linux 是最快的,但也有最大的差异.很多.但所有这些都可以有一个非常疯狂的波动:Mac 只有 200%,但 Windows 为 290%,Linux 为 810%!

So, Linux is the fastest but also has the most variance. By a LOT. But all of them can have a pretty wild swing: Only 200% for Mac, but 290% for Windows and 810% for linux!

它的执行时间真的有那么大吗?时间不够准确?还有什么我想念的吗?我在生成动画方面做了很多工作,我需要尽可能一致的时间.

Is it actually taking that much different time to execute? Is timeit not accurate enough? Is there something else I am missing? I'm working a lot with generating animations and I need as consistent time as possible.

推荐答案

您测量的时间非常短,然后即使某处发生的一点点事情也会产生很大的影响.

You are measuring very short times, and then even a little bit of something happening somewhere has a big impact.

我在我的机器(OS X、Core i7、Python 2.7)上运行了你的测试脚本并绘制了这个结果图:

I ran your test script on my machine (OS X, Core i7, Python 2.7) and made this plot of results:

您可以看到大部分时间的计时结果都非常一致,但也有一些孤立的算法需要花费更多时间(因为还有其他事情发生).

You can see that most of the time the timing results are very consistent, but there are isolated incidents of the algorithm taking much more time (because there is something else happening).

我对你的计时程序做了一个微小的调整:

I made a tiny adjustment to your timing procedure:

results=t.repeat(10, 1000)

所以,现在我们计时运行 1000 次函数调用.总时间是一样的,自然是(10000次调用):

So, now we are timing runs of 1000 function calls. The total amount of time is the same, naturally (10000 calls):

现在您可以看到性能更加可预测.可能你的部分时间不稳定是由于时间方法,而不是由于执行任何事情的时间不同.在现实世界的操作系统环境中,毫秒级计时是很困难的.即使你的电脑在什么都不做"的时候,它仍然在切换任务、做后台工作等.

Now you can see that the performance is much more predictable. It may be that part of your wobbly timings are due to the timing methodology, not due really different times to carry out anything. Millisecond-level timing is difficult in a real-world OS environment. Even when your computer is "doing nothing", it is still switching tasks, doing background jobs, etc.

我理解原点不是计算斐波那契数列.但如果是这样,那么选择正确的工具就会有所作为:

I understand the original point was not to calculate the Fibonacci numbers. But if it were, then choosing the right tool makes a difference:

import numpy as np

def fib(count):
    x = np.arange(count)
    a = (((1 + np.sqrt(5))/2) ** x - ((1 - np.sqrt(5)) / 2) ** x) / np.sqrt(5)
    a = a.astype('int')

这给出:

Min 0.120 | Max 0.471 | Max/Min 3.928 | Avg 0.125

十倍的速度提升.

关于这个答案中的图像,它们是用 matplotlib 绘制的.第一个是这样完成的:

About the images in this answer, they are plotted with matplotlib. The first one is done thus:

import matplotlib.pyplot as plt

# create a figure
fig = plt.figure()
# create axes into the figure
ax = fig.add_subplot(111)
# plot the vector results with dots of size 2 (points) and semi-transparent blue color
ax.plot(results, '.', c=(0, 0, 1, .5), markersize=2)

请参阅matplotlib 的文档.使用 IPythonpylab 最容易上手.

See the documentation of matplotlib. It is easiest to get started by using IPython and pylab.

这篇关于Python 在所有系统上的执行时间不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆