Home  >  Article  >  Backend Development  >  php calculate program execution time

php calculate program execution time

*文
*文Original
2017-12-26 11:04:324930browse

How does php calculate program execution time? This article mainly introduces the method of calculating program execution time in PHP. It takes the md5 function encryption running time as an example to analyze the PHP method of calculating function running time. I hope to be helpful.

The example in this article describes the method of calculating function execution time in PHP. Share it with everyone for your reference. The details are as follows:

We can record the start and end time before and after the program. The difference between the two times is the execution time of the program.


<?php
$long_str = "this is a test to see how much time md5 function takes to execute over this string";
// start timing from here
$start = microtime(true);
// function to test
$md5 = md5($long_str);
$elapsed = microtime(true) - $start;
echo "That took $elapsed seconds.\n";
?>


The running results are as follows:

That took 7.1525573730469E-6 seconds.

php Method to calculate function execution time and obtain subtlety


##

// 获得微妙方法
 function getMillisecond()
 {
   list($s1, $s2) = explode(&#39; &#39;, microtime());
   return (float)sprintf(&#39;%.0f&#39;, (floatval($s1) + floatval($s2)) * 1000);
 }


Principle: Record functions separately The start time and the end time, and then the time difference is the function execution time


<?php
 $start_time = microtime(true);
for($i=1;$i<=1000;$i++){
echo $i.&#39;<br>&#39;;
}
$end_time = microtime(true);
echo &#39;循环执行时间为:&#39;.($end_time-$start_time).&#39; s&#39;;
?>

Related recommendations:

As an interpreted language, PHP has a performance gap with compiled languages ​​​​Java and C++. How to improve the efficiency of PHP?

PHP performance analysis and experiment: Macroscopic analysis of performance

PHP performance analysis and Experiment: Micro Analysis of Performance

The above is the detailed content of php calculate program execution time. 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