[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)).read()
    else:
        os.popen('mv %s %s' % (f2, f1)).read()
print strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
# ./t.py 
Thu, 28 Jul 2011 03:18:55 +0000
Thu, 28 Jul 2011 03:18:56 +0000
Thu, 28 Jul 2011 03:19:38 +0000
/# ./t.py 
Thu, 28 Jul 2011 03:19:55 +0000
Thu, 28 Jul 2011 03:19:55 +0000
Thu, 28 Jul 2011 03:20:38 +0000

To many system call will have impact the performance,
and also the portable capability.

Wish this helps.
regards,
Stanley Huang

Comments

Popular posts from this blog

[Level 2] Shell Interactive With Zenity Episode 5 -- list

[Level 2] iif in Python