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(' ', microtime()); return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000); }
<?php $start_time = microtime(true); for($i=1;$i<=1000;$i++){ echo $i.'<br>'; } $end_time = microtime(true); echo '循环执行时间为:'.($end_time-$start_time).' s'; ?>
Related recommendations:
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!