• fibonacci(40) benchmark


    fibonacci(40) benchmark

    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 > java > go > scala > luajit > nodejs > pypy > ruby 1.9.3+ > lua > php > python > perl > ruby 1.8.x (T_T)

    LanguageTimes (user)PositionVersion
    c with -O20m0.202s#0i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1
    (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
    nodejs + cpp module0m1.001s#1v0.8.8, gcc -O2
    java0m1.305s#2Java(TM) SE Runtime Environment (build 1.6.0_35-b10-428-11M3811)
    Java HotSpot(TM) 64-Bit Server VM (build 20.10-b01-428, mixed mode)
    go0m1.667s#3go version go1.0.2
    scala0m1.808s#4Scala code runner version 2.9.2 -- Copyright 2002-2011, LAMP/EPFL
    luajit0m2.579s#5LuaJIT 2.0.0-beta10 -- Copyright (C) 2005-2012 Mike Pall.
    nodejs0m2.872s#6v0.8.8
    pypy0m30.010s#7Python 2.7.2 (341e1e3821ff, Jun 07 2012, 15:42:54) [PyPy 1.9.0 with GCC 4.2.1]
    ruby 1.9.x0m37.404s#8ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin12.1.0]
    lua0m40.709s#9Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
    jython0m53.699s#10Jython 2.5.2
    php1m17.728s#11PHP 5.4.6 (cli) (built: Sep 8 2012 23:49:53)
    python1m17.979s#12Python 2.7.2
    perl2m41.259s#13This is perl 5, version 12, subversion 4 (v5.12.4) built for darwin-thread-multi-2level
    ruby 1.8.x3m35.135s#14ruby 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 n;
     
    }
     
    return fibonacci(n - 2) + fibonacci(n - 1);
    }

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

    gcc -O0

    102334155

    real  
    0m4.145s
    user  
    0m3.892s
    sys
    0m0.012s

    gcc -O1

    102334155

    real  
    0m2.732s
    user  
    0m2.610s
    sys
    0m0.009s

    gcc -O2

    102334155

    real  
    0m0.249s
    user  
    0m0.202s
    sys
    0m0.004s

    gcc -O3

    102334155

    real  
    0m0.293s
    user  
    0m0.202s
    sys
    0m0.004s

    java

    java version "1.6.0_35" Java(TM) SE Runtime Environment (build 1.6.0_35-b10-428-11M3811) Java HotSpot(TM) 64-Bit Server VM (build 20.10-b01-428, mixed mode)

    public class Fibonacci {
     
    public static int fib(int n) {
       
    if (n < 2) {
         
    return n;
       
    } else {
         
    return fib(n - 1) + fib(n - 2);
       
    }
     
    }

     
    public static void main(String[] args) {
       
    System.out.print(fib(40) + "\n");
     
    }
    }
    102334155

    real  
    0m1.413s
    user  
    0m1.305s
    sys
    0m0.063s

    scala

    Scala code runner version 2.9.2 -- Copyright 2002-2011, LAMP/EPFL

    object Fibonacci {
     
    def fib(n: Int): Int = n match {
       
    case 0 | 1 => n
       
    case _ => fib(n -1) + fib(n-2)
     
    }

     
    def main(args: Array[String]) {
        println
    (fib(40));
     
    }
    }
    102334155

    real  
    0m1.909s
    user  
    0m1.808s
    sys
    0m0.121s

    go

    go version go1.0.2

    package main

    import "fmt"

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

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

    real  
    0m1.732s
    user  
    0m1.667s
    sys
    0m0.006s

    nodejs

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

    console
    .log(fibonacci(40));

    v0.2.6

    102334155

    real  
    0m4.959s
    user  
    0m4.699s
    sys
    0m0.028s

    v0.3.8

    102334155

    real  
    0m4.961s
    user  
    0m4.867s
    sys
    0m0.022s

    v0.4.12

    102334155

    real  
    0m7.544s
    user  
    0m7.219s
    sys
    0m0.030s

    v0.6.19

    102334155

    real  
    0m5.264s
    user  
    0m5.062s
    sys
    0m0.040s

    v0.6.20

    102334155

    real  
    0m5.413s
    user  
    0m5.195s
    sys
    0m0.038s

    v0.6.21-pre

    102334155

    real  
    0m5.081s
    user  
    0m4.906s
    sys
    0m0.037s

    v0.7.12

    102334155

    real  
    0m2.964s
    user  
    0m2.867s
    sys
    0m0.029s

    v0.8.0

    102334155

    real  
    0m3.034s
    user  
    0m2.868s
    sys
    0m0.029s

    v0.8.1

    102334155

    real  
    0m3.064s
    user  
    0m2.863s
    sys
    0m0.032s

    v0.8.3

    102334155

    real  
    0m3.024s
    user  
    0m2.855s
    sys
    0m0.030s

    v0.8.6

    102334155

    real  
    0m3.046s
    user  
    0m2.854s
    sys
    0m0.030s

    v0.8.7

    102334155

    real  
    0m3.258s
    user  
    0m2.933s
    sys
    0m0.033s

    v0.8.8

    102334155

    real  
    0m3.052s
    user  
    0m2.866s
    sys
    0m0.031s

    v0.9.1

    102334155

    real  
    0m3.190s
    user  
    0m2.852s
    sys
    0m0.032s

    nodejs + cpp module

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

    waf 1.5.16 (7610:7647M) v0.4.12

    102334155

    real  
    0m1.374s
    user  
    0m1.012s
    sys
    0m0.024s

    waf 1.5.16 (7610:7647M) v0.6.20

    102334155

    real  
    0m1.063s
    user  
    0m1.000s
    sys
    0m0.018s

    waf 1.5.16 (7610:7647M) v0.8.8

    102334155

    real  
    0m1.076s
    user  
    0m1.001s
    sys
    0m0.015s

    luajit

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

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

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

    real  
    0m2.847s
    user  
    0m2.579s
    sys
    0m0.013s

    using 'local'

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

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

    real  
    0m2.536s
    user  
    0m2.490s
    sys
    0m0.006s

    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 n
       
    return fibonacci(n - 2) + fibonacci(n - 1)

    print fibonacci(40)
    102334155

    real  
    0m34.082s
    user  
    0m30.010s
    sys
    0m0.303s

    ruby 1.9.x

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

    puts fibonacci
    (40)

    ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin12.1.0]

    102334155

    real  
    0m39.972s
    user  
    0m37.404s
    sys
    0m0.124s

    lua

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

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

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

    real  
    0m42.961s
    user  
    0m40.709s
    sys
    0m0.098s

    using 'local'

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

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

    real  
    0m37.178s
    user  
    0m35.887s
    sys
    0m0.081s

    python && jython

    Python 2.7.2

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

    print fibonacci(40)
    102334155

    real  
    1m23.989s
    user  
    1m17.979s
    sys
    0m0.303s

    Jython 2.5.2

    102334155

    real  
    0m58.651s
    user  
    0m53.699s
    sys
    0m1.945s

    php

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

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

    real  
    1m43.579s
    user  
    1m36.905s
    sys
    0m0.349s

    PHP 5.4.6 (cli) (built: Sep 8 2012 23:49:53)

    102334155

    real  
    1m25.155s
    user  
    1m17.728s
    sys
    0m0.261s

    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 $n;
     
    }
     
    return fibonacci($n - 2) + fibonacci($n - 1);
    }
    print fibonacci(40), "\n";
    102334155

    real  
    2m53.937s
    user  
    2m41.259s
    sys
    0m0.592s

    ruby 1.8.x

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

    puts fibonacci
    (40)

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

    102334155

    real  
    3m59.076s
    user  
    3m35.135s
    sys
    0m0.833s

    Conclusion

    • go is awsome!
    • nodejs is FAST, v0.8+ is FASTER.
    • luajit 2X faster than nodejs@0.6.x, Shocking.
    • ruby 1.9.x faster than lua.
  • 相关阅读:
    java的构造方法 java程序员
    No result defined for action cxd.action.QueryAction and result success java程序员
    大学毕业后拉开差距的真正原因 java程序员
    hibernate的回滚 java程序员
    验证码 getOutputStream() has already been called for this response异常的原因和解决方法 java程序员
    浅谈ssh(struts,spring,hibernate三大框架)整合的意义及其精髓 java程序员
    你平静的生活或许会在某个不可预见的时刻被彻底打碎 java程序员
    Spring配置文件中使用ref local与ref bean的区别. 在ApplicationResources.properties文件中,使用<ref bean>与<ref local>方法如下 java程序员
    poj1416Shredding Company
    poj1905Expanding Rods
  • 原文地址:https://www.cnblogs.com/lexus/p/2855227.html
Copyright © 2020-2023  润新知