2 ms·
Python 2.7 (Mid 2012 Macbook Pro, 2.5 GHz i5, 8GB, not using SSD) sum + xrange (consumes ~20MB virtual memory): $ time python2.7 -c "print sum(xrange(1,10
by neurostimulant 13y ago
Python 2.7 (Mid 2012 Macbook Pro, 2.5 GHz i5, 8GB, not using SSD)
sum + xrange (consumes ~20MB virtual memory):
$ time python2.7 -c "print sum(xrange(1,1000000001))"
500000000500000000
python2.7 -c "print sum(xrange(1,1000000001))" 11.06s user 0.02s system 99% cpu 11.089 total
reduce + xrange (consumes ~20MB virtual memory):
$ time python2.7 -c "print reduce(lambda a, b: a + b, xrange(1,1000000001))"
500000000500000000
python2.7 -c "print reduce(lambda a, b: a + b, xrange(1,1000000001))" 128.74s user 0.13s system 94% cpu 2:16.51 total
My machine swapping like crazy for more than an hour when I try using range(). I suspect it hasn't even finished allocating the list when I kill the process after it consumes >30GB virtual memory.
$ time python2.7 -c "print sum(range(1,1000000001))"