Skip to content Skip to sidebar Skip to footer

Inconsistent Execution Time In Python On All Systems

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

Solution 1:

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

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

enter image description here

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)

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

enter image description here

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')

This gives:

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

Ten-fold speed improvement.


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)

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

Solution 2:

I made a plot similar to @drV (give him the vote, he was first!) - the difference is that I sorted results so you could see a trend line. The hint is that even though the max is high, the average is low, so there are some outliers at the end.

enter image description here

I used pylab by just adding this to the bottom:

from pylab import *
results.sort()
plot(range(len(results)),results)
show()

Post a Comment for "Inconsistent Execution Time In Python On All Systems"