开发者

PHP profiling with microtime(): Negative time?

开发者 https://www.devze.com 2022-12-26 10:59 出处:网络
For a very simple profiling I use microtime() like this: $now = microtime(); for (...) { // do something

For a very simple profiling I use microtime() like this:

$now = microtime();
for (...) {
    // do something
    echo microtime() - $now;
    $now = microtime();
}

Now, the output of the echo line seems completely random, 开发者_Go百科that is, I expected fluctuations, but I didn't expect negative numbers showing up.

However, a typical result contains ~ 1/3 negative numbers. I confirmed this on Solaris (PHP 5.0.x) and WinVista (PHP 5.2.3).

What the heck is going on here? Have I invented accidently a time machine?


If you want to do operations on what is returned by microtime, you have to set the "get as float" parameter to true (it defaults to false).

http://www.php.net/manual/en/function.microtime.php

$now = microtime(true);
for (...) {
    // do something
    echo microtime(true) - $now;
    $now = microtime(true);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号