This is interesting; list()
to force an iterator to get the actual list is so much faster than [x for x in someList]
(comprehension).
Is this for real or is my test just too simple? Below is the code:
import time
timer = time.clock()
for i in xrange(90):
#localList = [x for x in xrange(1000000)] #Very slow, took me 6.8s
localList = list(xrange(1000000)) #Very fast, took me 0.9s
print localList[999999] #make sure list is really evaluated.
print "Total time: ", time.clock() - timer
See Question&Answers more detail:os