3 ms·
It compares the time to Python, based on reduce(lambda...). The lambda x, y: x+y is slower than operator.add: % /usr/bin/time -p python -c 'reduce(lambda x,y
by dalke 10y ago
It compares the time to Python, based on reduce(lambda...). The lambda x, y: x+y is slower than operator.add:
% /usr/bin/time -p python -c 'reduce(lambda x,y: x+y, xrange(1000000000))'
real 140.16
user 140.10
sys 0.05
% /usr/bin/time -p python -c 'import operator; reduce(operator.add, xrange(1000000000))'
real 67.04
user 67.02
sys 0.01
Python is not optimized for functional programming. The idiomatically relevant comparison would be:
% /usr/bin/time python -c 'sum(xrange(1000000000))'
11.99 real 11.98 user 0.01 sys
% /usr/bin/time pypy -c 'sum(xrange(1000000000))'
1.38 real 1.35 user 0.02 sys
That of course still doesn't beat pre-computation.