Home  >  Article  >  Backend Development  >  How to calculate script execution time in PHP

How to calculate script execution time in PHP

青灯夜游
青灯夜游Original
2019-01-30 10:07:537993browse

In PHP, you can calculate the script execution time by using the built-in function microtime(), which can be used to return the current Unix timestamp in microseconds. The following article will show you how the microtime() function calculates script execution time.

How to calculate script execution time in PHP

The script execution time in PHP is actually the time required to execute the PHP script. To calculate script execution time, clock time is used instead of CPU execution time. Calculating the clock time before the script is executed and after the script is executed will help in calculating the script execution time.

Clock time can use the microtime() function. First use it before starting the script, then use it at the end of the script; finally using the formula (End_time - Start_time), you can calculate the execution time of the script.

Example:

<?php 
header("content-type:text/html;charset=utf-8"); 
// 开始的时钟时间(秒)
$start_time = microtime(true); 
$a=1; 
  
// 起动回路
for($i = 1; $i <=1000; $i++) 
{ 
    $a++; 
}  
  
// 结束的时钟时间(秒)
$end_time = microtime(true); 
  
// 计算脚本执行时间
$execution_time = ($end_time - $start_time); 
  
echo " 脚本执行时间 = ".$execution_time." 秒"; 
?>

Output:

How to calculate script execution time in PHP

Note: The microtime() function measures microseconds Returns the time in units; execution time is not fixed and depends on the processor.

The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to calculate script execution time in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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