简体   繁体   English

PHP 微时间的奇怪问题

[英]Strange issue with PHP's microtime

I'm trying to use php's microtime() to benchmark how long two loops in my code take, to see which one runs faster.我正在尝试使用 php 的 microtime() 来衡量我的代码中的两个循环所花费的时间,以查看哪个循环运行得更快。 Here's my code:这是我的代码:

$now = microtime();

//loop1  here

$elapsed = microtime() - $now;
echo "Elapsed: $elapsed <br />";

When I run this for one loop, I get the result in negative numbers, something like:当我运行一个循环时,我得到负数的结果,例如:

Elapsed: -0.120572 

This is strange, since microtime() should always be bigger than its value a few microseconds earlier?这很奇怪,因为microtime()应该总是比它早几微秒的值大?

For the second loop, the value is always positive:对于第二个循环,该值始终为正:

Elapsed: 0.005483 

What's going on here?这里发生了什么?

根据http://au1.php.net/manual/en/function.microtime.php使用microtime(true) as false 将 microtime 作为字符串返回,并且您在字符串上执行数学运算而不是作为数字..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM