PHP:函数的时间速度,但是太快了?

发布于 2024-08-19 13:53:12 字数 348 浏览 2 评论 0原文

我真的很想测试正则表达式等的速度,在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鹿港巷口少年归 2024-08-26 13:53:12

定时器就是这样的。在函数周围包裹一个循环。如果要测量微秒,请运行 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.

戒ㄋ 2024-08-26 13:53:12

usleep 函数需要以微秒为单位的时间,您指定的值非常低,请尝试增加该值:

usleep(2000000); // wait for two seconds

usleep 函数在 Windows 系统上也很棘手。

或者您可以简单地使用 sleep 函数,并将秒数指定为其参数,例如:

sleep(10); // wait for 10 seconds

The usleep function need time in microsecond, you are specifying very low value, try increasing that:

usleep(2000000); // wait for two seconds

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:

sleep(10); // wait for 10 seconds
夏有森光若流苏 2024-08-26 13:53:12

无法重现,并且没有人拥有比我更快的计算机。 16 个执行线程,八个核心,i7 架构。您的 PHP 版本或操作系统一定不是最佳的。这正是运行您的代码。

`--> php tmp.php
Did nothing in 0.00011587142944336 seconds
.-(/tmp)---------------------------------------------------(pestilence@pandemic)-

这是 usleep(1000000)...(1 秒)

`--> php tmp.php
Did nothing in 1.0000479221344 seconds
.-(/tmp)---------------------------------------------------(pestilence@pandemic)-

仅供参考,我使用的是 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.

`--> php tmp.php
Did nothing in 0.00011587142944336 seconds
.-(/tmp)---------------------------------------------------(pestilence@pandemic)-

This is with usleep(1000000)... (1 second)

`--> php tmp.php
Did nothing in 1.0000479221344 seconds
.-(/tmp)---------------------------------------------------(pestilence@pandemic)-

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文