LoginSignup
4
4

More than 5 years have passed since last update.

フィボナッチで各種言語をベンチマーク

Last updated at Posted at 2016-01-15

参考

計測

計測
sh -c "TIME=%U time コマンド"

結果

lang time(s)
C (gcc 5.3.1) -O3 0.46
C (gcc 5.3.1) -O2 0.48
C (gcc 5.3.1) 0.78
go 1.5.1(build) 0.78
go 1.5.1 1.09
crystal 0.10.2(build) 1.36
crystal 0.10.2 1.46
Node.js 4.2.4 1.69

超えられない壁

lang time(s)
php 7.0.2 15.14
ruby 2.2.3 16.07
python 2.7 32.89
python 3.5 35.52
php 5.6.16 39.70
gawk 4.1.3 43.64

RaspberryPi2のpython2.7だと6m26.196sかかりました。


ソース

C

fib.c
#include <stdio.h>
int fib(int n)
{
    if (n <= 1) {
        return n;
    }
    return fib(n-1) + fib(n-2);
}

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

Go

fib.go
fib.go 
package main

import "fmt"

func fib(n uint) uint {
    if n <= 1 {
        return n
    }
    return fib(n-1) + fib(n-2)
}

func main() {
    fmt.Println(fib(40))
}

Ruby, Crystal

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

puts fib(40)

Python2,3

fib.py
def fib(n):
    if n <= 1:
        return n
    return fib(n - 1) + fib(n - 2)

print(fib(40))

php

fib.php
<?php
function fib($n) {
  if ($n <= 1) {
     return $n;
  }
  return fib($n - 1) + fib($n - 2);
}

print fib(40);

awk

fib.awk
BEGIN {
    printf "%d\n", fib(40)
}

function fib(n) {
    if (n <= 1) return n
    return fib(n - 1) + fib(n - 2)
}
4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4