PHP_IloveU (2) php clothes and food parent: Java and PHP efficiency competition: Fibonacci series

Source: Internet
Author: User
PHP_IloveU (2) php food and clothing parents: Java and PHP efficiency competition: PHP_ I love U (1) php food and clothing parents: Java and PHP efficiency competition:

Fibonacci series

Fibonacci

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


  • (It should be 1. the Wikipedia formula is incorrect !?!)
  • (N? 2)

  • Write the Java code first this time:

    Class fb {

    Static int f1b (int x ){

    If (0 = x) | (1 = x) {return 1 ;}
    Int;
    A = f1b (x-1) + x;
    System. out. println ();
    Return;
    }
    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 the end time
    System. out. println ("Runing Time:" + (endTimeMs-startTimeMs) + "ms ");

    } // Main
    } // Class fb


    PHP code:


    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 );

    // Code for testing (time) efficiency;


    Fun1 ($ x0 );

    $ T2 = microtime (true );
    Echo ($ t2-$ t1) * 1000). 'Ms ';


    Result:

    Java :......

    499500

    Time: 102.167238ns

    Ms time: 104 ms


    PHP :...

    ......

    499500

    Time (ms): 161.00978851318 ms


    Result:

    JAVA vs PHP

    104 ms vs 161 ms

    1574 ms vs 909 ms

    Of course, Java won ......


    But considering that java code should be compiled with Javac ......

    PHP code runs directly with php.exe.

    So after the calculation of the 999 (or 9999) Fibonacci series (both approximate) 1: 1.6 Efficiency ratio ...... PHP is acceptable.


    In short: php I continues to love U (you )!

    Related Article

    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.