Monday, October 20, 2014

Comparing speeds using different import methods in python

test_argv.py
from sys import argv                                                                              
i=0                                                                                               
while i < 10000000:                                                                               
    len(argv)                                                                                     
    i+=1

test_local.py
import sys                                                                                        
                                                                                                  
local = sys.argv                                                                                  
i=0                                                                                               
while i < 1E7:                                                                                    
    len(local)                                                                                    
    i+=1

test_sys.py
import sys                                                                                        
i=0                                                                                               
while i < 1E7:                                                                                    
    len(sys.argv)                                                                                 
    i+=1

time python test_argv.py; time python test_local.py; time python test_sys.py

real    0m1.081s
user    0m1.081s
sys    0m0.000s

real    0m1.260s
user    0m1.248s
sys    0m0.012s

real    0m1.420s
user    0m1.420s
sys    0m0.000s

No comments:

Post a Comment