How to calculate program running time in php

autoload
Release: 2023-03-09 09:40:02
Original
5627 people have browsed it

How to calculate program running time in php

1. Functions used

a. Use the function microtime ()

microtime ( bool $get_as_float = ? ) : mixed
Copy after login
  • $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)
Copy after login
  • 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(&#39; &#39;,microtime());  
echo microtime(); 
 /*········以下是代码区·········*/  
 for($i=0;$i<1000000;$i++)//这里是计算循环一百万次所需要的时间为:0.116秒。
 {   $i;  
 }  
 /*········以上是代码区·········*/   
 //程序运行时间  
 $endtime = explode(&#39; &#39;,microtime());  
 $thistime = $endtime[0]+$endtime[1]-($starttime[0]+$starttime[1]); 
 $thistime = round($thistime,3);  
 echo "本网页执行耗时:".$thistime." 秒。".time(); ?>
Copy after login

3. Output:

0.58607300 1617257726本网页执行耗时:0.017 秒。1617257726
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template