Crystal lang vs NodeJS vs Golang vs Julia vs rust vs LuaJIT vs C Fibonacci benchmark

I tested Http speed of different languages to see wich of them is the faster, by default, for http. Now, for benchmark purpose, I'm testing Fibonacci speed of Crystal, NodeJS, Golang, Julia and C.

Methodology

The code is compiled (with better optimizations possible) if necessary and executed 10 times. The best result of each is reported in the table.

Results

We are testing fibonacci(45). Here are best results of each :

Language Version Result
C gcc 4.9.2 3.575s
Crystal v0.20.1 6.981s
Rust 1.14.0 7.808s
Julia v0.5.0 9.657s
LuaJIT 2.1.0-beta2 10.150s
Golang go1.7.4 linux/amd64 10.249s
NodeJS v7.3 15.122s

Update : Tested with NodeJS 7.3 and Rust 1.14.0

#implementation

  • Crystal code
def fibonacci(n)
  if n < 2
    n
  else
    fibonacci(n - 1) + fibonacci(n - 2)
  end
end

puts fibonacci(45)

Compilation and test :

crystal build --release fibo.cr
time ./fibo
  • C code
#include <stdio.h>

int fibonacci(int n) {
  if (n < 2) {
    return n;
  }
  return fibonacci(n - 2) + fibonacci(n - 1);
}

int main() {
  printf("%d\n", fibonacci(45));
  return 0;
}

Compilation and test :

gcc -O7 fibo.c -o fibo
time ./fibo
  • Julia code
fibonacci(n) = n < 2 ? n : fibonacci(n-1) + fibonacci(n-2)
println(fibonacci(45))

Test :

julia fibo.jl
  • Golang code
package main
import "fmt"

func fibonacci(n int) int {
  if n < 2 {
   return n
  }
  return fibonacci(n-2) + fibonacci(n-1)
}
func main(){
        fmt.Printf("%v\n",fibonacci(45));
}

Compilation and test :

go build fibo.go
time ./fibo
  • Rust
fn fibonacci(n: i32) -> i32 {
    if n < 2 { return n }
    else { return fibonacci(n - 1) + fibonacci(n - 2) }
}
fn main()
{
        println!("{}",fibonacci(45));
}

Compilation and test :

rustc -O -C no-prepopulate-passes fibo.rs
time ./fibo
  • NodeJS code
function fibonacci(n) {
  if (n < 2) {
    return n;
  }
  return fibonacci(n - 2) + fibonacci(n - 1);
}

console.log(fibonacci(45));

Test :

time node --use-strict fibo.js
  • LuaJIT code
local function fibonacci(n)
        if n < 2 then return n
        else return fibonacci(n-1)+fibonacci(n-2)
        end
end
io.write(fibonacci(45), "\n");

Test :

time luajit fibo.lua

Conclusions

C is the faster. Crystal is 2x slower than C, like Rust. Julia is 2.5x slower than C, Go is 3x slower than C, like LuaJIT, wich is the Lua interpreter with a JIT. NodeJS, wich is an interpreted language based on the V8 engine, is 5x slower than C, For extreme computation, C is the better choice. If you are a Ruby dev and want something faster, you can try Crystal. If you are using R and want a good alternative, Julia does the job. If you need security, Rust is made for you. For those who want an interpreted language, NodeJS and LuaJIT are well, even if LuaJIT is faster.