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

WBOY
Release: 2016-06-23 13:44:19
Original
870 people have browsed it

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

    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!