python 3.3 ,2.7 and 2.6 - benchmark inconsistent results. how fix that?

I use this python scripts:

python2.6 and 2.7

for i in xrange(1000000):print i

python3.3

for i in range(1000000):print(i)

Results:

python 2.7

real    1m15.973s
user    0m3.392s
sys 0m6.384s

python 2.6

real    1m15.860s
user    0m3.064s
sys 0m6.296s

With python 3.3 I test the script many times and I receive this diff of running .

python 3.3

real    1m34.235s
user    0m10.668s
sys 0m8.988s

real    1m29.431s
user    0m10.304s
sys 0m9.512s

real    1m12.528s
user    0m10.568s
sys 0m9.004s

real    1m4.193s
user    0m9.688s
sys 0m8.812s

real    1m18.332s
user    0m9.728s
sys 0m9.132s

After that I try again python 2.6 and I got this:

real    0m45.003s
user    0m3.056s
sys 0m5.956s

What is the best way to benchmark 2 python scripts 3.3 and 2.7 (or 2.6).

Upvotes: 1

Views: 223

Answers (3)

perror
perror

Reputation: 7406

There are several ways to benchmark Python programs. At least, I can come with two serious ways. You can find an expanded version of what I say in these slides here. You also may benefit from this video from a talk about Python profiling from PyCon 2013 (from Amjith Ramanujam).

cProfile module

The cProfile module gives you an insight of the time spent in every procedure of your program. It can be manipulated in a very efficient and precise manner. But, the drawback of it is that you cannot trust the execution time it gives for each procedure but its relative time spent compared to the others.

Using cProfile is simply done like this:

python -m cProfile ./mypythonprogram.py

If you know gprof, it will give you a similar output but for a Python program.

timeit module

The timeit module is intended to really evaluate the time the program spend in total. On the contrary to cProfile, there is no extra instrumentation of each procedure and, thus, no slowdown during the execution.

def foo ():
    for i in range (10):
        i = i * i

from timeit import Timer
max_iter = 10000
t = Timer ("foo()", "from __main__ import foo")
print ("foo(%i): %f seconds" %(max_iter, t.timeit(max_iter)))

And, you call it like this within the command line:

$> python2.7 timeit_example.py
foo(10000): 0.012774 seconds
$> python3.2 timeit_example.py
foo(10000): 0.014030 seconds

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1121992

Use the timeit module to compare small Python snippets. It avoids the common pitfalls, making timing values comparable.

However, you are mostly timing write speed to sys.stdout when timing print i (and in Python 3, encoding to the terminal codec).

Upvotes: 7

Cjkjvfnby
Cjkjvfnby

Reputation: 134

Don't test with "print", use something else.

Upvotes: 1

Related Questions