.

Python print performance

Perhaps this is kind of obvious, but I only realized this today: using the print statement in Python can cost you a lot of time!

When I'm hacking on a piece of code containing a large for-loop in some algorithm, I'm often printing the current iteration to the console to keep a tab on the progress.

Consider this trivial example:

j = 0
for i in xrange(4096**2):
    j += i
    print i

This outputs a fast scrolling list of integers so I can keep track of progress, but this results in the following running time on the console:

$ time python print_time.py
...
...
16777215

real    11m5.059s
user    1m16.077s
sys     1m8.568s

Running the same code without the print statement takes a lot less time:

$ time python print_time.py

real    0m13.585s
user    0m13.569s
sys     0m0.008s

Please correct me if I'm wrong, but my guess is it's not because of the performance of the print statement, but the ability of the console to process all that output.

posted on 13 Feb, 2010 to Python » view comments
blog comments powered by Disqus