PHP:函数的时间速度,但是太快了?
我真的很想测试正则表达式等的速度,在 php.net 上有这个例子:
$time_start = microtime(true);
// Sleep for a while
usleep(100); // Or anything for that matter..
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Did nothing in $time seconds\n";
编辑:我的意思是玩一个大循环的函数来代替 usleep()。它总是显示一个始终低于 1 的非常随机的数字。它没有显示任何值得基准测试的东西!
I've REALLY been wanting to test speeds of regex etc. and on php.net it has this example:
$time_start = microtime(true);
// Sleep for a while
usleep(100); // Or anything for that matter..
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Did nothing in $time seconds\n";
EDIT: What I meant was to play a large loop of functions in place of usleep(). It always shows a very random number that is always under 1. It shows nothing worth a benchmark!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
定时器就是这样的。在函数周围包裹一个循环。如果要测量微秒,请运行 10^6 次;如果要测量毫秒,请运行 10^3 次。不要期望获得超过 2 位小数的精度。
Timers are like that. Wrap a loop around the function. Run it 10^6 times if you want to measure microseconds, 10^3 times if you want milliseconds. Don't expect to get more than 2 decimal digits of precision.
usleep 函数需要以微秒为单位的时间,您指定的值非常低,请尝试增加该值:
usleep 函数在 Windows 系统上也很棘手。
或者您可以简单地使用 sleep 函数,并将秒数指定为其参数,例如:
The usleep function need time in microsecond, you are specifying very low value, try increasing that:
usleep function is also known to be tricky on Windows systems.
Or you can simply use the sleep function with seconds to be specified as its parameter eg:
无法重现,并且没有人拥有比我更快的计算机。 16 个执行线程,八个核心,i7 架构。您的 PHP 版本或操作系统一定不是最佳的。这正是运行您的代码。
这是 usleep(1000000)...(1 秒)
仅供参考,我使用的是 PHP 5.3.0,这与 5.0 相比(在本例中)没有任何区别。
您的代码执行时间是否少于一秒?如果是这样,那就可以解释你的基准。当我为一篇文章分析某些内容时,我会运行例程一万次……有时甚至数百万次。如今计算机速度很快。
就“随机”部分而言,这就是为什么我采用大型迭代的几个样本并将它们平均在一起的原因。把标准差也放进去也没什么坏处。另外,请记住在测试时不要流式传输 MP3 或播放视频。 :p
Cannot reproduce, and no one has a faster computer than me. 16 threads of execution, eight cores, i7 architecture. Your PHP build or OS must be suboptimal. This is running your code exactly.
This is with usleep(1000000)... (1 second)
FYI, I was using PHP 5.3.0, not that it makes any difference (in this case) past 5.0.
Is your code taking less than a second to execute? If so, that'd explain your benchmarks. When I profile something for an article, I run my routine 10,000's of times... sometimes millions. Computers are fast these days.
As far as the "random" part goes, this is why I take several samples of large iterations and average them together. It doesn't hurt to throw the standard deviation in there too. Also, remember not to stream MP3s or play video while testing. :p