1. Functions used
a. Use the function microtime ()
microtime ( bool $get_as_float = ? ) : mixed
$get_as_float
which can be omitted when set to TRUE
When , it is specified that the function should return a floating point number, otherwise it returns a string. Default is FALSE.
Return value: Default return string , where sec
is the since Unix
epoch (0:00:00 January 1, 1970 GMT) The number of seconds since, microsec
is the microsecond part. If the argument is set to TRUE
, returns a floating point number representing the current time in seconds since the Unix
epoch, accurate to microseconds.
b. Use the function explode()
explode(separator,string,limit)
separator to specify where to split characters string.
string The string to be split.
limit specifies the number of array elements returned (optional)
2. The code is as follows :
<?php //程序运行时间 $starttime = explode(' ',microtime()); echo microtime(); /*········以下是代码区·········*/ for($i=0;$i<1000000;$i++)//这里是计算循环一百万次所需要的时间为:0.116秒。 { $i; } /*········以上是代码区·········*/ //程序运行时间 $endtime = explode(' ',microtime()); $thistime = $endtime[0]+$endtime[1]-($starttime[0]+$starttime[1]); $thistime = round($thistime,3); echo "本网页执行耗时:".$thistime." 秒。".time(); ?>
3. Output:
0.58607300 1617257726本网页执行耗时:0.017 秒。1617257726
PS: For the sake of neatness of the program, we can use this code This function can also be achieved by writing it as a class, introducing it when used, instantiating the class before the program starts, and calling a method at the end.
Recommendation: 《2021 PHP interview questions summary (collection)》《php video tutorial》
The above is the detailed content of How to calculate program running time in php. For more information, please follow other related articles on the PHP Chinese website!