Home

Awesome

Recursive Fibonacci Benchmark using top languages on Github

Top 10: JavaScript, Python, Java, TypeScript, C#, Php, C++, C, Shell, Ruby reference

Others: Go, Rust, Swift, Crystal, Pony, Ada, Pascal, Fortran, Kotlin, Clojure, Scala, Mono, R, Dart, Julia, D, Nim, Cython, Python3, PyPy, Ruby jit, OCaml, Lisp, Haskell, Erlang, Elixir, Escript, Dart, Scheme, Lua, Perl, Perl6, Bash, Emoji

The code performs a recursive fibonacci to the 47th position with the result of 2,971,215,073.

Fibonacci can be written many different ways. The goal of this project is to compare how each language handles the exact same code.

Here is the Ruby version:

def fib(n)
  return n if n <= 1
  fib(n - 1) + fib(n - 2)
end

puts fib(47)

Here is the Crystal version:

def fib(n)
  return n if n <= 1
  fib(n - 1) + fib(n - 2)
end

puts fib(47_u64)

Too keep a level playing field, only common "release" flags are used in the compilation step. This allows for compiler optimizations like inlining and constant propogation but removes anything considered dangerous i.e. bypassing out of bounds checks.

All tests are run on:

How to run them

You can run the tests using Docker: docker run -it drujensen/fib

By default, it will compile and run all languages 5 times. Totals are calculated by adding the average compile and run times.

To only run a subset of the languages, provide a list of extensions and optionally the count:

docker run -it drujensen/fib ./run.sh s,c,cpp,go,rs,swift 5

To run in the background using nohup:

nohup docker run drujensen/fib:latest > results.txt 2>&1 &

Results

Last benchmark was ran on January 18, 2022

Natively compiled, statically typed

LanguageTotalCompileTime, sRunTime, sExt
C6.865gcc -O3 -o fib fib.c0.058./fib6.807c
Fortran6.893gfortran -O3 -o fib fib.f030.119./fib6.774f03
C++6.954g++ -O3 -o fib fib.cpp0.126./fib6.828cpp
Cython7.114cython -3 --embed -o fib.pyx.c fib.pyx && gcc -O3 -o fib fib.pyx.c $(pkg-config --cflags --libs python)0.297./fib6.817pyx
Nim7.337nim c -d:release fib.nim0.920./fib6.416nim
Assembly7.401gcc -no-pie -O3 -o fib fib.s0.025./fib7.376s
Ada7.645gnat make -O3 -gnatp -o fib fib.adb0.213./fib7.433adb
Rust7.652rustc -C opt-level=3 fib.rs0.638./fib7.014rs
Pascal8.845fpc -O3 -Si ./fib.pas0.078./fib8.766pas
Pony9.117ponyc -s -b fib -p ./fib.pony1.038./fib8.079pony
D9.481ldc2 -O3 -release -flto=full -of=fib fib.d0.612./fib8.869d
Swift10.568swiftc -O -g fib.swift0.474./fib10.094swift
OCaml10.729ocamlopt -O3 -o fib fib.ml0.294./fib10.435ml
V10.752v -prod -o fib fib.v4.352./fib6.399v
Go12.486go build fib.go0.600./fib11.885go
Haskell12.544rm ./fib.o && ghc -O3 -o fib fib.hs0.001./fib12.543hs
Crystal17.900crystal build --release fib.cr4.940./fib12.960cr
Lisp17.996sbcl --load fib.lisp1.704./fib16.293lisp
Dart Compiled30.942dart compile exe -o fib ./fib.dart5.058./fib25.884dartc
Cobol4380.728cobc -x -O3 -o fib ./fib.cbl0.133./fib4380.596cbl

VM compiled bytecode, statically typed

LanguageTotalCompileTime, sRunTime, sExt
Scala9.047scalac Fib.scala0.942scala Fib8.104scala
Java10.391javac Fib.java0.894java Fib9.497java
Kotlin11.721kotlinc Fib.kt2.097java FibKt9.624kt
C# (Mono)16.107mcs Fib.cs0.491mono Fib.exe15.616mono
C#16.218dotnet build -c Release -o ./bin2.162dotnet ./bin/fib.dll14.056cs
Erlang28.868erlc +native +'{hipe,[o3]}' fib.erl0.500erl -noinput -noshell -s fib28.368erl

VM compiled before execution, mixed/dynamically typed

LanguageTime, sRunExt
Dart11.286dart fib.dartdart
Julia11.318julia -O3 fib.jljl
Lua Jit16.382luajit fib.lualuajit
Clojure23.448clojure -M fib.cljccljc
Elixir28.604ERL_COMPILER_OPTIONS='[native,{hipe, [o3]}]' elixir Fib.exsexs
Node30.232node fib.jsjs
Python3 (PyPy)37.827pypy3 fib.pypypy
Ruby (jit)51.793ruby --jit fib.rbrbjit

Interpreted, dynamically typed

LanguageTime, sRunExt
Scheme66.668guile fib.scmscm
Php118.468php fib.phpphp
Lua169.710lua fib.lualua
Ruby223.653ruby fib.rbrb
Janet292.366janet ./fib.janetjanet
Python507.845python fib.pypy
Python3527.247python3 fib.pypy3
Perl1085.181perl fib.plpl
Tcl1580.804tclsh fib.tcltcl
R2413.682R -f fib.rr
Raku2841.391rakudo fib.rakuraku
Escript3448.282escript fib.eses

Versions

All compilers are installed using apt or asdf on Ubuntu 20.04 docker image.

languageversion
ada9.3.0
assembly9.3.0
bash5.0.0
crystal1.3.0
clojure1.10.3.1040
cython0.29.26
dart2.15.1
dotnet-core6.0.101
elixir1.12.0
elm0.19.1
erlang24.2
fortran9.3.0
g++9.3.0
gcc9.3.0
golang1.17.5
guile3.0.7
haskell9.2.1
janet1.19.2
javaopenjdk-17
julia1.7.1
K3.6
kotlin1.6.10
ldc21.2.4
lua5.4.3
luaJIT2.1.0
mono6.8.0
nim1.6.2
nodejs17.3.0
ocaml4.11.1
pascal3.0.4
perl5.34.0
php8.1.1
pony0.38.1
powershellv7.0.0
python3.10.1
pypy7.3.7
qb641.5
R4.1.2
rakudo2021.12
ruby3.1.0
rust1.57.0
sbcl2.11.1
scala3.1.0
swift5.5.2
tcl8.6.10
v0.2.2