PHP_I love U之(2)php衣食父母: Java與PHP效率比拼之一:斐波那契數列

來源:互聯網
上載者:User
PHP_I love U之(1)php衣食父母: Java與PHP效率比拼之一:

斐波那契數列

Fibonacci

解釋見:http://zh.wikipedia.org/wiki/%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0%E5%88%97


  • ( 應該是1 , 維基的公式錯了!?!)
  • (n?2)

  • 這次先寫 Java的代碼:

    class fb {

    static int f1b (int x) {

    if ((0==x)||(1==x) ) { return 1 ;}
    int a;
    a=f1b(x-1)+x;
    System.out.println( a);
    return a;
    }
    public static void main(String[] args) {


    long startTime=System.nanoTime(); //star
    long startTimeMs=System.currentTimeMillis(); //
    //doSomeThing(); //Coding
    f1b(999);
    long endTime=System.nanoTime(); //end
    System.out.println("Run Timming:"+(endTime-startTime)+"ns");
    long endTimeMs=System.currentTimeMillis(); //擷取結束時間
    System.out.println("Runing Time: "+(endTimeMs-startTimeMs)+"ms");

    }//main
    }//class fb


    PHP的代碼:


    function Fun1($x) //$x)
    {
    if (0==$x) { return 1;echo "\r\n";}
    if (1==$x) { return 1;echo "\r\n";}

    $b1= $x + Fun1( $x-1 ) ;
    echo $b1;
    echo "\r\n";
    return $b1 ;
    }


    $x0=999;//100;
    $t1 = microtime(true);

    //要測試(時間)效率的代碼;


    Fun1($x0);

    $t2 = microtime(true);
    echo (($t2-$t1)*1000).'ms';


    結果:

    Java:......

    499500

    Time: 104177238ns

    MS time: 104ms


    PHP:...

    ......

    499500

    Time(MS): 161.00978851318ms


    結果:

    JAVA vs PHP

    104ms vs 161ms

    1574ms vs 909ms

    當然是 Java勝出……


    但考慮到 java的代碼 要用 Javac 編譯一遍……

    而PHP的代碼 直接 用php.exe 直接就運行了

    所以 999 次(或9999次)斐波那契數列 計算之後 (都近似為) 1 : 1.6 的效率比 …… PHP還是能接受的吧


    總之:PHP I 繼續 love U(you)!

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.