TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Adding a billion numbers in Rust

6 pointsby omn1over 8 years ago

2 comments

dalkeover 8 years ago
It compares the time to Python, based on reduce(lambda...). The lambda x, y: x+y is slower than operator.add:<p><pre><code> % &#x2F;usr&#x2F;bin&#x2F;time -p python -c &#x27;reduce(lambda x,y: x+y, xrange(1000000000))&#x27; real 140.16 user 140.10 sys 0.05 % &#x2F;usr&#x2F;bin&#x2F;time -p python -c &#x27;import operator; reduce(operator.add, xrange(1000000000))&#x27; real 67.04 user 67.02 sys 0.01 </code></pre> Python is not optimized for functional programming. The idiomatically relevant comparison would be:<p><pre><code> % &#x2F;usr&#x2F;bin&#x2F;time python -c &#x27;sum(xrange(1000000000))&#x27; 11.99 real 11.98 user 0.01 sys % &#x2F;usr&#x2F;bin&#x2F;time pypy -c &#x27;sum(xrange(1000000000))&#x27; 1.38 real 1.35 user 0.02 sys </code></pre> That of course still doesn&#x27;t beat pre-computation.
grok2over 8 years ago
But 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?