5 ms·
Some compilers should be able to optimize the loop away and just display the final value when the code runs. Edit: discussed previously https://news.ycombinato
by andy99 3y ago
Some compilers should be able to optimize the loop away and just display the final value when the code runs.
Edit: discussed previously https://news.ycombinator.com/item?id=35398298 https://news.ycombinator.com/item?id=35398298
- nomilk 3y agoGreat find! I think the loop is essential otherwise it's an unfair comparison. But TIL that compilers can do that, so it's very interesting. I'm now wondering if there's a way to force compilers not to do that (so as to make for fair performance comparisons - e.g. it sounds like rust may do the calculation at compile time and simply print a constant at run time).
- Someone 3y agoThe first thing to do is pass the iteration count as an argument. That makes it less likely that a compiler optimizes this, but doesn’t guarantee it won’t do it (it is fairly easy to prove how often the loop runs and what the value of n is afterwards) Edit: for getting an idea about what compilers can do, read https://kristerw.blogspot.com/2019/04/how-llvm-optimizes-geometric-sums.html https://kristerw.blogspot.com/2019/04/how-llvm-optimizes-geo... for examples of what LLVM (the backend of various language implementations, including clang) does.
- andy99 3y agoAlso, what is he really trying to benchmark? Would it be informative to generate 10^9 random numbers and add them up, or is there a specific goal of finding how fast a counter is incremented?
- nomilk 3y agoIt’s an arbitrary benchmark, inspired by this: https://youtube.com/watch?v=VioxsWYzoJk https://youtube.com/watch?v=VioxsWYzoJk
- andy99 3y agoI got curious and wrote it in assembly (Disclaimer, I haven't written any assembly code since university, it was mostly chatGPT). See the attached gist. This should be the gold standard for a "fair" loop, one that's actually looping in raw assembly. https://gist.github.com/rbitr/0649972657f5d0d959eeb71a5b3e0206 https://gist.github.com/rbitr/0649972657f5d0d959eeb71a5b3e02...
- Someone 3y agoThat removes the binary-to-decimal conversion of the result, which easily could take a significant fraction of running time when using a smart compiler.
- andy99 3y agoI don't think that is material, it definitely isn't for code that's actually running the loop. I did just try timing it with the pipe to hexdump and of course it makes no difference. One could do the same or just modify the code to include conversion if worried about the impact.
- nomilk 3y agoFound this fun comparison, but this time measuring development time: https://www.youtube.com/watch?v=3PcIJKd1PKU https://www.youtube.com/watch?v=3PcIJKd1PKU