TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ask HN: Strange execution times of Javascript and PHP

1 点作者 icode大约 14 年前
Hello HN,<p>just for fun I executed this in javascript:<p><pre><code> function test() { var r=1; for (var i=0;i&#60;10000000;i++) { r*=1.0000001; } return r; } var start = (new Date).getTime(); test(); diff = (new Date).getTime() - start; </code></pre> that gives me about 80ms<p>And this in PHP:<p><pre><code> function test() { $r=1; for ($i=0;$i&#60;10000000;$i++) { $r*=1.0000001; } return $r; } $start = microtime(true)*1000; $r=test(); echo "$r\n"; $stop = microtime(true)*1000; echo $stop-$start."\n"; </code></pre> That gives me about 1300 ms on the command line and about 5000 ms when executed by apache.<p>Any ideas to explain these differences?

1 comment

Khao大约 14 年前
I just hosted your php script over here : <a href="http://khao.kodingen.com/test.php" rel="nofollow">http://khao.kodingen.com/test.php</a> and it's a lot faster but still not perfect. I get about 721ms when I run it and when I run the javascript version in firebug it's a lot slower : 1188ms.<p>I guess the browser you're using is compiling the javascript more efficiently (probably by doing some kind of crazy optimization in the for loop) while PHP is not as good for optimizing for loops.
评论 #2332774 未加载