5 ms·
Adding a billion numbers in Rust
- dalke 10y agoIt 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.
- grok2 10y agoBut what is the code generated when the value of N is not known at compile time. i.e it is a user provided value (which is the more interesting case since it happens all the time) -- does the fold still get abstracted into a for-loop kind of thing in the generated code?