Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何以微秒为单位获取持续时间_Php_Time_Php 5.3_Microtime - Fatal编程技术网

Php 如何以微秒为单位获取持续时间

Php 如何以微秒为单位获取持续时间,php,time,php-5.3,microtime,Php,Time,Php 5.3,Microtime,我想测量php脚本的指定部分需要多长时间,例如 $startTime = microtime(true); //some operations which needs 0.1 to 100 seconds to run $endTime = microtime(true); 如何将持续时间以微秒为单位获取为整数 这不起作用: $duration = (int)($endTime - $startTime); 如果希望微秒为整数: $duration = round(($endTime - $

我想测量php脚本的指定部分需要多长时间,例如

$startTime = microtime(true);
//some operations which needs 0.1 to 100 seconds to run
$endTime = microtime(true);
如何将持续时间以微秒为单位获取为整数

这不起作用:

$duration = (int)($endTime - $startTime);

如果希望微秒为整数:

$duration = round(($endTime - $startTime) * 1000000)

为什么不使用
round
<代码>$duration=round($endTime-$startTime)。。。它返回一个float类型,但它将返回一个整数…你得到的时间是一个整数,你只是对什么是整数有一个非常基本的误解。使用(int)强制转换时,小于1的任何值都将设置为0。您可能需要更像$duration=$endTime-$startTime$持续时间=数字\格式($duration,2);等等,让我们来定义你所说的“微秒为整数”是什么意思。。。如果脚本运行时间为.23秒,是否希望看到230?@dleiftah是的,我需要的是微秒(2300),而不是秒,因为你需要毫秒。这将是战俘(10,6)。完美!也适用于:(int)($endTime-$startTime)*pow(10,6))作为注释。。。整型铸造总是四舍五入,所以有可能两者会产生不同的结果。。。