代码之家  ›  专栏  ›  技术社区  ›  Boldewyn

PHP profiling with microtime():负时间?

  •  41
  • Boldewyn  · 技术社区  · 16 年前

    microtime() 这样地:

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

    echo 线条似乎完全随机,也就是我预期的波动,但我 期待 负数 出现了。

    然而,一个典型的结果包含大约1/3的负数。我在Solaris(PHP5.0.x)和WinVista(PHP5.2.3)上证实了这一点。

    这到底是怎么回事?我是不是偶然发明了时间机器?

    1 回复  |  直到 13 年前
        1
  •  69
  •   Arkh    16 年前

    如果要对microtime返回的内容执行操作,必须将“get as float”参数设置为true(默认为false)。

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

    $now = microtime(true);
    for (...) {
        // do something
        echo microtime(true) - $now;
        $now = microtime(true);
    }
    
    推荐文章