Using the print statement in Python can be very expensive!

Perhaps this is kind of obvious, but I only realized this today. 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

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

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

$ time python

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

It's not because of the performance of the print statement, but the ability of the console to process all that output.

blog comments powered by Disqus