ntanitime
March 2nd, 2012, 05:46 PM
Hi,
I run this script first in Python IDE 2.7 and after in IDE 3.2.
import time
import sys
xxx=range(10000)
def timer(func, *pargs, **kargs):
start=time.clock()
for i in xxx:
res = func(*pargs, **kargs)
ela = time.clock() -start
return (ela,res)
def forLoop():
res = []
for x in xxx:
res.append(abs(x))
return res
def listComp():
return [abs(x) for x in xxx]
def mapCall():
return list(map(abs, xxx))
def genExpr():
return list(abs(x) for x in xxx)
def genFun():
def gen():
for x in xxx:
yield abs(x)
return list(gen())
print(sys.version)
for test in (forLoop, listComp, mapCall, genExpr, genFun):
ela, resu = timer(test)
print ('-'*33)
print ('%-9s: %.5f => [%s...%s]' % (test.__name__, ela, resu[0], resu[-1]))Python 2.7:
>>>
2.7.2+ (default, Oct 4 2011, 20:06:09)
[GCC 4.6.1]
---------------------------------
forLoop : 13.03000 => [0...9999]
---------------------------------
listComp : 6.99000 => [0...9999]
---------------------------------
mapCall : 5.42000 => [0...9999]
---------------------------------
genExpr : 9.70000 => [0...9999]
---------------------------------
genFun : 9.74000 => [0...9999]
>>>
2.7.2+ (default, Oct 4 2011, 20:06:09)
[GCC 4.6.1]
---------------------------------
forLoop : 12.42000 => [0...9999]
---------------------------------
listComp : 6.99000 => [0...9999]
---------------------------------
mapCall : 5.80000 => [0...9999]
---------------------------------
genExpr : 9.72000 => [0...9999]
---------------------------------
genFun : 9.72000 => [0...9999]Python 3.2:
>>>
3.2.2 (default, Sep 5 2011, 21:17:14)
[GCC 4.6.1]
---------------------------------
forLoop : 13.89000 => [0...9999]
---------------------------------
listComp : 8.61000 => [0...9999]
---------------------------------
mapCall : 6.29000 => [0...9999]
---------------------------------
genExpr : 11.42000 => [0...9999]
---------------------------------
genFun : 11.27000 => [0...9999]
>>>
3.2.2 (default, Sep 5 2011, 21:17:14)
[GCC 4.6.1]
---------------------------------
forLoop : 14.18000 => [0...9999]
---------------------------------
listComp : 8.58000 => [0...9999]
---------------------------------
mapCall : 6.47000 => [0...9999]
---------------------------------
genExpr : 11.61000 => [0...9999]
---------------------------------
genFun : 11.44000 => [0...9999]Why Python 2.7 is faster than 3.2 ???
is it ok ?
I run this script first in Python IDE 2.7 and after in IDE 3.2.
import time
import sys
xxx=range(10000)
def timer(func, *pargs, **kargs):
start=time.clock()
for i in xxx:
res = func(*pargs, **kargs)
ela = time.clock() -start
return (ela,res)
def forLoop():
res = []
for x in xxx:
res.append(abs(x))
return res
def listComp():
return [abs(x) for x in xxx]
def mapCall():
return list(map(abs, xxx))
def genExpr():
return list(abs(x) for x in xxx)
def genFun():
def gen():
for x in xxx:
yield abs(x)
return list(gen())
print(sys.version)
for test in (forLoop, listComp, mapCall, genExpr, genFun):
ela, resu = timer(test)
print ('-'*33)
print ('%-9s: %.5f => [%s...%s]' % (test.__name__, ela, resu[0], resu[-1]))Python 2.7:
>>>
2.7.2+ (default, Oct 4 2011, 20:06:09)
[GCC 4.6.1]
---------------------------------
forLoop : 13.03000 => [0...9999]
---------------------------------
listComp : 6.99000 => [0...9999]
---------------------------------
mapCall : 5.42000 => [0...9999]
---------------------------------
genExpr : 9.70000 => [0...9999]
---------------------------------
genFun : 9.74000 => [0...9999]
>>>
2.7.2+ (default, Oct 4 2011, 20:06:09)
[GCC 4.6.1]
---------------------------------
forLoop : 12.42000 => [0...9999]
---------------------------------
listComp : 6.99000 => [0...9999]
---------------------------------
mapCall : 5.80000 => [0...9999]
---------------------------------
genExpr : 9.72000 => [0...9999]
---------------------------------
genFun : 9.72000 => [0...9999]Python 3.2:
>>>
3.2.2 (default, Sep 5 2011, 21:17:14)
[GCC 4.6.1]
---------------------------------
forLoop : 13.89000 => [0...9999]
---------------------------------
listComp : 8.61000 => [0...9999]
---------------------------------
mapCall : 6.29000 => [0...9999]
---------------------------------
genExpr : 11.42000 => [0...9999]
---------------------------------
genFun : 11.27000 => [0...9999]
>>>
3.2.2 (default, Sep 5 2011, 21:17:14)
[GCC 4.6.1]
---------------------------------
forLoop : 14.18000 => [0...9999]
---------------------------------
listComp : 8.58000 => [0...9999]
---------------------------------
mapCall : 6.47000 => [0...9999]
---------------------------------
genExpr : 11.61000 => [0...9999]
---------------------------------
genFun : 11.44000 => [0...9999]Why Python 2.7 is faster than 3.2 ???
is it ok ?