fibonacci(40) benchmark

Node.js is Cancer show a wrong way to use nodejs. But the test code Fibonacci is so funny. I implement the fibonacci function in other Dynamic Languages for comparison testing.

Languages

Dynamic

Static

If you want to help add more dynamic languagues, please leave the implement code in comments.

Results

(^_^) c > go > luajit > nodejs > pypy > lua > python > php > perl > ruby (T_T)

LanguageTimes (user)PositionVersion
c with -O20m0.187s#0 i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
node + cpp module0m1.417s#1 v0.8.8, gcc -O2
go0m1.664s#2 go version go1.0.2
luajit0m2.463s#3 LuaJIT 2.0.0-beta10 -- Copyright (C) 2005-2012 Mike Pall.
nodejs0m2.826s#4 v0.8.8
pypy0m29.650s#5 Python 2.7.2 (341e1e3821ff, Jun 07 2012, 15:42:54) [PyPy 1.9.0 with GCC 4.2.1]
lua0m42.944s#6 Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
python1m19.029s#7 Python 2.7.2
php1m43.355s#8 PHP 5.3.13 with Suhosin-Patch (cli) (built: Jun 20 2012 17:05:20)
perl2m30.444s#9 This is perl 5, version 12, subversion 4 (v5.12.4) built for darwin-thread-multi-2level
ruby3m32.074s#10 ruby 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin12.0]

lua use local function will get better performance.

fibonacci(40) benchmark result:

c

i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)

#include <stdio.h>

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

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

gcc -O0

165580141

real  
0m3.908s
user  
0m3.884s
sys
0m0.004s

gcc -O1

165580141

real  
0m1.783s
user  
0m1.773s
sys
0m0.003s

gcc -O2

165580141

real  
0m0.192s
user  
0m0.185s
sys
0m0.002s

gcc -O3

165580141

real  
0m0.199s
user  
0m0.187s
sys
0m0.002s

go

go version go1.0.2

package main

import "fmt"

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

func main
() {
  fmt
.Println(fibonacci(40))
}
165580141

real  
0m1.686s
user  
0m1.666s
sys
0m0.004s

nodejs

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

console
.log(fibonacci(40));

v0.4.12

165580141

real  
0m7.252s
user  
0m7.211s
sys
0m0.018s

v0.6.20

165580141

real  
0m5.152s
user  
0m5.127s
sys
0m0.027s

v0.8.8

165580141

real  
0m2.894s
user  
0m2.826s
sys
0m0.020s

nodejs + cpp module

var fibonacci = require('./build/Release/cppfibonacci').fibonacci;
console
.log(fibonacci(40));

waf 1.5.16 (7610:7647M) v0.4.12

165580141

real  
0m1.474s
user  
0m1.427s
sys
0m0.015s

waf 1.5.16 (7610:7647M) v0.6.20

165580141

real  
0m1.444s
user  
0m1.420s
sys
0m0.013s

waf 1.5.16 (7610:7647M) v0.8.8

165580141

real  
0m1.436s
user  
0m1.417s
sys
0m0.014s

luajit

LuaJIT 2.0.0-beta10 -- Copyright (C) 2005-2012 Mike Pall. http://luajit.org/

function fibonacci(n)
 
if n < 2 then
   
return 1
 
end
 
return fibonacci(n - 2) + fibonacci(n - 1)
end

io
.write(fibonacci(40), "\n")
165580141

real  
0m2.582s
user  
0m2.547s
sys
0m0.005s

using 'local'

local function fibonacci(n)
 
if n < 2 then
   
return 1
 
end
 
return fibonacci(n - 2) + fibonacci(n - 1)
end

io
.write(fibonacci(40), "\n")
165580141

real  
0m2.470s
user  
0m2.463s
sys
0m0.003s

pypy

Python 2.7.2 (341e1e3821ff, Jun 07 2012, 15:42:54) [PyPy 1.9.0 with GCC 4.2.1]

def fibonacci(n):
   
if n < 2:
       
return 1
   
return fibonacci(n - 2) + fibonacci(n - 1)

print fibonacci(40)
165580141

real  
0m30.819s
user  
0m29.650s
sys
0m0.196s

lua

Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio

function fibonacci(n)
 
if n < 2 then
   
return 1
 
end
 
return fibonacci(n - 2) + fibonacci(n - 1)
end

io
.write(fibonacci(40), "\n")
165580141

real  
0m44.095s
user  
0m42.944s
sys
0m0.101s

using 'local'

local function fibonacci(n)
 
if n < 2 then
   
return 1
 
end
 
return fibonacci(n - 2) + fibonacci(n - 1)
end

io
.write(fibonacci(40), "\n")
165580141

real  
0m38.479s
user  
0m37.655s
sys
0m0.082s

python

Python 2.7.2

def fibonacci(n):
   
if n < 2:
       
return 1
   
return fibonacci(n - 2) + fibonacci(n - 1)

print fibonacci(40)
165580141

real  
1m21.027s
user  
1m19.029s
sys
0m0.200s

php

PHP 5.3.13 with Suhosin-Patch (cli) (built: Jun 20 2012 17:05:20)

<?php
function fibonacci($n) {
 
if ($n < 2) {
   
return 1;
 
}
 
return fibonacci($n - 2) + fibonacci($n - 1);
}
echo fibonacci
(40)."\n";
?>
165580141

real  
1m46.118s
user  
1m43.355s
sys
0m0.250s

perl

This is perl 5, version 12, subversion 4 (v5.12.4) built for darwin-thread-multi-2level

sub fibonacci {
 
my $n = shift;
 
if ($n < 2) {
   
return 1;
 
}
 
return fibonacci($n - 2) + fibonacci($n - 1);
}
print fibonacci(40), "\n";
165580141

real  
2m33.350s
user  
2m30.444s
sys
0m0.340s

ruby

ruby 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin12.0]

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

puts fibonacci
(40)
165580141

real  
3m37.177s
user  
3m32.074s
sys
0m0.507s

Conclusion

::...
免责声明:
当前网页内容, 由 大妈 ZoomQuiet 使用工具: ScrapBook :: Firefox Extension 人工从互联网中收集并分享;
内容版权归原作者所有;
本人对内容的有效性/合法性不承担任何强制性责任.
若有不妥, 欢迎评注提醒:

或是邮件反馈可也:
askdama[AT]googlegroups.com


点击注册~> 获得 100$ 体验券: DigitalOcean Referral Badge

订阅 substack 体验古早写作:


关注公众号, 持续获得相关各种嗯哼:
zoomquiet


自怼圈/年度番新

DU22.4
关于 ~ DebugUself with DAMA ;-)
粤ICP备18025058号-1
公安备案号: 44049002000656 ...::