Performance effect of using print statements in Python script
Tried doing it in a very simple script just for fun, the difference is quite staggering:
In large.py:
target = open('target.txt', 'w')for item in xrange(4000000): target.write(str(item)+'\n') print item
Timing it:
[gp@imdev1 /tmp]$ time python large.pyreal 1m51.690suser 0m10.531ssys 0m6.129sgp@imdev1 /tmp]$ ls -lah target.txt -rw-rw-r--. 1 gp gp 30M Nov 8 16:06 target.txt
Now running the same with "print" commented out:
gp@imdev1 /tmp]$ time python large.py real 0m2.584suser 0m2.536ssys 0m0.040s
Yes it affects performance.I wrote a small program to demonstrate-
import timestart_time=time.time()for i in range(100): for j in range(100): for k in range(100): print(i,j,k)print(time.time()-start_time)input()
The time measured was-160.2812204496765Then I replaced the print statement by pass. The results were shocking. The measured time without print was- 0.26517701148986816.