Method of time counting
------------------------
import time
start = time.clock()
…
…
elapsed = (time.clock() - start)
print elapsed
-------------------------
results
#----------------fibonaci-----------------
# run time 1.5e-05
#a, b = 0, 1
#v=[]
#while b < 4000000:
# a, b = b, a+b
# v.append(a)
# run time 4.2e-05
#v = [1,0];
#while v[0] < 1000000:
# v=sum([v], [v[0]+v[1]])
# run time 3e-05
#v = [0,1];
#while v[-1] < 1000000:
# v=v+ [v[-1]+v[-2]]
# run time 2.3e-05
#v=[0,1];
#while v[-1] < 1000000:
# v[-1:-1],v[-1]=[v[-1]],v[-1]+v[-2]
# run time 1.5e-05
#v=[0,1];
#while v[-1] < 1000000:
# v.append(v[-1]+v[-2])
the 5th is the best style, both fast and elegant.
No comments:
Post a Comment