PHP中使用微秒的脚本执行时间示例
在PHP中,大部分是由Unix时间戳格式表示的时间,和Unix时间戳是与最小测量的最小时间单位(秒)。对于某些应用程序,这是不准确的,所以你可以叫瞬时()返回当前Unix时间戳和微妙的数量。这个函数的原型如下:复制代码代码如下所示:
混合的瞬时({布尔get_as_float }); / /返回当前Unix时间戳和微妙的数
您可以为这个函数提供可选的布尔参数。If you don't provide this parameter when calling, this function returns a string in the form of msec sec.SEC is the number of seconds from the UNIX era to the present, and msec is a subtle part, and the two part of the string is returned in seconds.If the get_as_float parameter is given and its value is equivalent to TRUE, microtime () will return a floating-point number.In front of the decimal point, it is represented in a timestamp format, while the decimal point indicates a subtle value.But note that the parameter get_as_float is newly added in the PHP5.0 version, so in the previous version of PHP5, you can't directly use this parameter to request a floating point number directly.In the following example, by calling the microtime () fun取两次,需要运行PHP脚本的时间计算,代码如下:
复制代码代码如下所示:
< PHP
生命计算脚本运行时类
类定时器{
私人时间= 0美元; / /脚本保存在执行时间的开始(在保存表单)微秒
私人的停止时间= 0美元; / /保存该脚本的执行时间(以微秒的形式保存结束)
在开始时间的脚本中,脚本调用得到微秒值。
函数start(){
美元-> StartTime =瞬时(true); / /分配到属性$ StartTime成员的时间
}
在脚本脚本的结尾处,带有结束时间的值
函数停止(){
美元->停止时间为微秒(true); / /分配给属性的成员的时间停止时间美元
}
同一个脚本访问时间的返回/ /或两次不同
函数花费(){
计算后5户在4 4保留返回
回轮(($这->美元->停止时间-开始时间),4);
}
}
$计时器=新计时器();
在脚本文件的执行开始时调用这个方法。
usleep(1000); / /主题的剧本,例如一毫秒可以睡这里
在脚本文件中调用此方法
执行使用的脚本。
>
在上面的脚本,一类计时器宣布计算脚本的执行时间。你需要调用的开始()方法在类在脚本执行的开始时间当脚本开始执行,并调用停止()方法在类的脚本执行结束的时候得到的脚本在脚本结束运行时,访问了()类中的方法,你可以得到所需的运行脚本的时间。