[Level 3] Python performance.
Days ago, I discussed the coding style of python, and I use one example to present what I want you to know. The most 2 ways to use of renaming a file are: 1. Use rename method os moudle [ os.rename(f1, f2) ], 2. Use external command by popen method[ os.popen('mv f1 f2') ]. Some people might think there are not much different between these two ways. But after a stress test, you will realize it did has different between these two ways. The sample code and test run are as the following. #!/bin/env python import os, sys from time import gmtime, strftime n = 10000 f1 = '/1' f2 = '/2' os.popen('touch %s' % f1).read() print strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()) for i in range(n): if os.path.exists(f1): os.rename(f1, f2) else: os.rename(f2, f1) print strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime()) for i in range(n): if os.path.exists(f1): os.popen('mv %s %s' % (f1, f2))...