Php_i love u (2) php food and clothing parents: Java and PHP efficiency battle one: Fibonacci sequence

Source: Internet
Author: User
Php_i love u (1) php Food and clothing parents: Java and PHP efficiency battle one:

Fibonacci sequence

Fibonacci

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


  • (should be 1, Wiki's formula is wrong!?! )
  • (n?2)

  • This time write the Java code First:

    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 (); Get End time
    System.out.println ("runing Time:" + (Endtimems-starttimems) + "MS");

    }//main
    }//class FB


    Code for PHP:


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

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


    $x 0=999;//100;
    $t 1 = microtime (true);

    The code to test (time) efficiency;


    Fun1 ($x 0);

    $t 2 = Microtime (true);
    Echo (($t 2-$t 1) *1000). ' Ms ';


    Results:

    Java:......

    499500

    Time:104177238ns

    MS time:104ms


    Php:...

    ......

    499500

    Time (MS): 161.00978851318ms


    Results:

    JAVA vs PHP

    104ms vs 161ms

    1574ms vs 909ms

    Java wins, of course ...


    But consider the Java code to be compiled with Javac again ...

    The PHP code runs directly with Php.exe.

    So 999 times (or 9,999 times) The Fibonacci sequence is calculated (both approximate) 1:1.6 efficiency ratio ... PHP is still acceptable.


    Anyway: PHP I continue love U!

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.