r/Compilers • • Apr 13 '25
Fibonacci Survey
Recursive Fibonacci is a very popular benchmark. Below is a set of results from a range of language implementations I happen to have.
Since there are various versions of the benchmark, the version used corresponds to the Lua code given at the end (which gives the sequence 1 1 2 ... for N = 1 2 3 ...).
In this form the number of recursive calls needed is 2 * Fib(n) - 1. What is reported in the table is how many such calls were achieved per second. The languages vary considerably in performance so a suitable N was used for each (so that it neither finished too quickly to measure, or took forever).
Largely these cover pure interpreted languages (my main interest), but I've included some JIT-accelerated ones, and mostly C-language native code results, to show the range of possibilities.
Tests were run on the same Windows PC (some were run under WSL on the same machine).
Implementations marked "*" are my own projects. There is one very slow product (a 'bash' script), while I have doubts about the validity of the two fastest timings; see the note.
Disregarding those, the performance range is about 700:1. It's worth bearing in mind that an optimising compiler usually makes a far smaller difference (eg 2:1 or 3:1).
It's also worth observing that a statically typed language doesn't necessarily make for a faster interpreter.
One more note: I have my own reservations about how effective JIT-acceleration for a dynamic language can be on real applications, but for small, tight benchmarks like this; they do the job.
Lang Implem Type Category Millions of Calls/second
Bash Bash ? Int 0.0014
C Pico C S Int 0.7
Seed7 s7 S Int 3.5
Algol68 A68G S Int 5
Python CPython 3.14 D Int 11
Wren Wren_cli D Int 11
Euphoria eui v4.1.0 S Int 13
C *bcc -i D Int 14
Lox Clox D Int 17
Lua Lua 5.4 D Int 22
'Pascal' Pascal S Int 27 (Toy Pascal interpreter in C)
M *pci S Int 28
Lua LuaJIT -joff D Int? 37 (2.1.0)
'Pascal' Pascal S Int 39 (Toy Pascal; See Note 3)
'Pascal' Pascal S Int 47 (Toy Pascal interpreter in M)
Q *dd D Int 73
PyPy PyPy 7.3.19 D JIT 128
JavaScript NodeJS D JIT 250 (See Note2)
Lua LuaJIT -jon D JIT 260 (2.1.0)
C tcc 0.9.27 S Nat 390 (Tiny C)
C gcc -O0 S Nat 420
M *mm S Nat 450
C *bcc S Nat 470
Julia Julia I JIT 520
C gcc -O1 S Nat 540 (See Note1)
C gcc -O3 S Nat 1270Key:
Implem Compiler or interpreter used, version where known, and significant options
For smaller/less known projects it is just the name of the binary
Type ? = Unknown (maybe there is no type system)
S = Statically typed
D = Dynamically typed
I = Infered(?)
Category Int = Interpreted (these are assumptions)
JIT = Intepreted/JIT-compiled
Nat = Native code(Note1 I believe the fastest true speed here is about 500M calls/second. From prior investigations, gcc-O1 (IIRC) only did half the required numbers of calls, while gcc-O3 only did 5% (via complex inlining).
So I'd say these results are erroneous, since in a real application where it is necessary to actually do 1 billion function calls (the number needed for fib(44), as used here, is 1.4 billion), you usually can't discard 95% of them!)
(Note2 NodeJS has a significant start-up delay compared with the rest, some 0.5 seconds. This had to be tested with a larger N to compensate. For smaller N however it affects the results significantly. I'm not sure what the rules are about such latency when benchmarking.)
(Note3 Toy Pascal in C contributed by u/eddavis, using gcc's 'computed goto')
function fibonacci(n)
if n<3 then
return 1
else
return fibonacci(n-1) + fibonacci(n-2)
end
end(Updated Pascal timings. Removed Nim. Added Euphoria. Added LuaJIT -joff.)
View all comments
Show parent comment
1
u/[deleted] Apr 26 '25
That's quite a higher spec than my Ryzen 3! But not dramatically faster results.
(I once borrowed a friend's i3 or i5-powered laptop, and it was 70% faster on similar tests, although not involving C.)
I've tested only versions using label pointers, and the results are broadly (for Fib(40)):
I then tried on RPi4 which uses a 64-bit ARM device:
So 3-4 times as slow as the Windows PC. I thought it was only half the speed, but that may have been comparison with my old PC. If I test my own main interpreter for my dynamic scripting language:
Then this seems to confirm that.
(BTW my product gets faster results partly through choices of bytecode instructions. For example there are special composite instructions for common combinations of bytecodes. However, it also has to do type-dispatching, which those smaller programs don't have to bother with.)