Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
4
Help us understand the problem. What is going on with this article?
@tukiyo3

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

More than 5 years have passed since last update.

参考

計測

計測
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
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
tukiyo3
最近はC#とBusyBoxと農業してます

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
4
Help us understand the problem. What is going on with this article?