dongshao1021 2014-10-20 05:42
浏览 88

在PHP中获取微观或毫秒的时差

I am trying to find time required to execute some function using php. I want accuracy of milli or micro seconds but i get difference in seconds.

The code is as follows:

<?php

$time_start = microtime(true);


usleep(2000000);

$time_end = microtime(true);

$time = $time_end - $time_start;

echo "Did nothing in $time micro seconds
";


?>

The output i get is 2 micro seconds but it should be 2000000 micro seconds according to program.

If i keep usleep(100) it will give me 0 difference.

Please help me with some solution to this.

  • 写回答

2条回答 默认 最新

  • dongyuan9892 2014-10-20 05:49
    关注

    The result of microsecond(true) is a float, "which represents the current time in seconds" and "accurate to the nearest microsecond" (http://php.net/microtime). Meaning decimals before the comma are in seconds and the decimals after the comma represent the microseconds.

    Therefore the example in the manual is more correct than your variant:

    echo "Did nothing in $time seconds
    ";
    

    If the execution takes exactly two seconds, it will output:

    Did nothing in 2 seconds
    

    If the execution takes two seconds and 500 milliseconds, it will output:

    Did nothing in 2.5 seconds
    
    评论

报告相同问题?

悬赏问题

  • ¥15 制裁名单20240508芯片厂商
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致