Home > Article > Backend Development > A simple example of php calculating program running time
Here is an introduction to microtime(). This function
microtime() 函数返回当前 Unix 时间戳和微秒数。 语法:microtime(get_as_float)参数: get_as_float 描述: 如果给出了 get_as_float 参数并且其值等价于 TRUE,该函数将返回一个浮点数。 说明: PHP函数microtime()仅在支持 gettimeofday() 系统调用的操作系统下可用。 如果调用时不带可选参数,本函数以 "msec sec" 的格式返回一个字符串, 其中 sec 是自 Unix 纪元(0:00:00 January 1, 1970 GMT)起到现在的秒数,msec 是微秒部分。 字符串的两部分都是以秒为单位返回的。
microtime() is not used much, but it cannot be ignored. Know this function, it returns the current Unix timestamp and microseconds. For example: echo microtime(); will return: 0.08845800 1376983061. So you can use the explode function to split it into an array with spaces as the identifier, then $starttime[0]= 0.08845800 (number of microseconds), $starttime[1]=1376983061 (current number of seconds, equivalent to the result obtained by time()).
Sample code: The code is as follows:
<?php //程序运行时间 $starttime = explode(' ',microtime()); echo microtime(); /*········以下是代码区·········*/ for($i=0;$i<1000000;$i++){ $i; } /*········以上是代码区·········*/ //程序运行时间 $endtime = explode(' ',microtime()); $thistime = $endtime[0]+$endtime[1]-($starttime[0]+$starttime[1]); $thistime = round($thistime,3); echo "本网页执行耗时:".$thistime." 秒。".time(); ?>
Finally, subtract the two times, and then use the round() function to keep the required execution time Decimal places are ok. For example, here is the calculation time required to loop one million times: 0.116 seconds, as shown below:
The above is the detailed content of A simple example of php calculating program running time. For more information, please follow other related articles on the PHP Chinese website!