PHP program execution time detection
We sometimes need to judge the execution time and execution efficiency of the program.
The implementation ideas are as follows:
<?php //記錄開始時間 //記錄結(jié)整時 // 開始時間 減去(-) 結(jié)束時間 得到程序的運行時間 ?>
But don’t forget that the program runs too fast. It's as fast as a moment of only 0.00000 seconds. At this time, everyone has to record a special function:
mixed microtime ([ bool $get_as_float ] )
microtime() This function can return the current Unix timestamp and microseconds.
Parameters:
If you pass in true, a floating point type time will be returned, which is convenient for participating in operations.
Let’s simulate an example of detecting the execution time of a function to test the efficiency of a certain function:
<?php //開始時間 $time_start = microtime(true); //循環(huán)一萬次 for($i = 0 ; $i < 10000 ; $i++){ //你可以用上,mktime() 生成一個昨天的時間 //再用strtotime() 生成一個昨天的時間 //對比兩個函數(shù)認(rèn)的效率高 } //結(jié)整時間 $time_end = microtime(true); //相減得到運行時間 $time = $time_end - $time_start; echo "這個腳本執(zhí)行的時間為 $time seconds\n"; ?>
The final output result is the execution time of our actual function. You can compare several times to see the final result.
Whoever has less time can use which function frequently in actual work.