3 ms·
The "use cases and opinions" section is pretty nice, and I agree with it. I would expect Rust and Swift to have the same execution performance on Fibonacci. In
by pcwalton 11y ago
The "use cases and opinions" section is pretty nice, and I agree with it.
I would expect Rust and Swift to have the same execution performance on Fibonacci. In fact, I'd expect them to generate essentially identical LLVM IR. The fact that they have differences in performance leads me to believe that it's some sort of "LLVM IR optimization didn't trigger because of a bug" sort of issue—perhaps the optimization pass that converts recursive functions to loops.
I have to say, though: Beware of Fibonacci as a benchmark. I believe it's vulnerable to a compiler that optimizes it to the closed form solution [1]. I don't think compilers do this optimization today, but if you popularize a Fibonacci benchmark you will create market pressure for them to implement it and ruin your results. :)
[1]: https://en.wikipedia.org/wiki/Fibonacci_number#Closed-form_expression https://en.wikipedia.org/wiki/Fibonacci_number#Closed-form_e...
- Veedrac 11y agoThe closed form expression for Fibonacci numbers is actually not great, since the precision required ends up taking up your time. A simpler way would just be to unroll the recursion until overflow and cache the small number of results.
- jerf 11y ago"Beware of Fibonacci as a benchmark." It seems to me one of the dangers of its use is that people tend to think that it measures "math speed" or "execution speed" or something. But the fib calculation itself is done with small integer addition, things that will be about one cycle in any compiled language, and which is also what I've called "easy mode" for a JIT before. Since the calculation under test is so tiny compared to the overhead, the benchmark is really measuring the overhead, and due to the simplicity of the test, very vulnerable to special purpose optimizations skewing the results vs. what you might expect for real. This isn't "useless", because that overhead can matter, but unless you have a program that is going to spend an irreducible and large amount of its time being forced to make static function calls only to do tiny amounts of work per function call relative to the function overhead, the information is only a quite tiny part of understanding the performance characteristics of a language. If one has optimized even a half-decently compiled language's program down to where the function call overhead is your biggest problem, one is to be congratulated on some fine engineering, and I expect that you'll know what to do next if you need more speed as you've already demonstrated significant competence. You want to have a bit more fun, benchmark the local equivalent of "dynamic" dispatch. That'll spread the languages out quite a bit more. For fun, toss in Python or something too.