strange sum behavior

simon place simon_place at whsmithnet.co.uk
Sun Feb 22 10:05:03 EST 2004


while playing about with inheriting from list to be able to cache macro 
properties i noticed this, the rate of summing a list seems to be over linear?

its nearly 3 times faster to sum the sums of smaller lists?


from time import clock

l=range(0,1000000)

start=clock()
print sum(l),
print clock()-start

# now sum a list of the sums of 1000 slices.
start=clock()
print sum([sum(l[x:x+1000]) for x in xrange(0,len(l),1000)]),
print clock()-start

# repeat
start=clock()
print sum(l),
print clock()-start

# output from 500MHz AMD K62 64Mb Python 2.3.3 (#51, Dec 18 2003, 20:22:39) 
[MSC v.1200 32 bit (Intel)] on win32
#499999500000 1.89348044721
#499999500000 0.731985115406
#499999500000 1.90818149818



More information about the Python-list mailing list