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