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!