This article mainly introduces PHP to get the target function execution time example, the need friend can refer to the following
A class is written to test the execution time of the target function. The following is the definition code for the class: code is as follows: <?php/** * class Efficiencytester * efficiency tester, test function run time * @version 1.0 201 3.04.13 * @author Kross */class Efficiencytester { /** * var $testTimes * test times */ Private $testTimes = 1000; /** * function getTime () * get timestamp * according to time mode @param $timeModel time mode, default: microsecond * @return int timestamp */ Private function GetTime ($timeModel = ' ms ') { if ($timeModel = = ' ms ') { &NBS P return Microtime (); } else if ($timeModel = = ' S ') { return time ()   ; } else { return microtime ();   { } /** * function testonce () * test objective function once, return to run time * @param $functionName target function name * @param $timeModel time mode, default: microsecond * @retur N Double objective function time (very random) */ Public Function testonce ($functionName, $timeModel = ' MS ') { $startMicroTime = $this->gettime ($timeModel); & nbsp $functionName (); $endMicroTime = $this->gettime ($timeModel); $costMicroTime = $endMicroTime-$startMicroTime; return $costMicroTime; } /** * function test () * Testing the target function multiple times, return to run time (average) * @pa Ram $functionName target function name * @param $timeModel time mode, default: microseconds * @return Double objective functions time of the operation &NBSP ; * * &NBsp Public function test ($functionName, $timeModel = ' MS ') { $totalMicroTimes = 0; & nbsp for ($i = 1; $i <= $this->testtimes; $i + +) { $totalMicroTimes + = $t His->testonce ($functionName); } return $totalMicroTimes/$this->testtimes; }?> The following is the test code for the class: the code is as follows: <?php require_once (' ... /class/efficiencytester.class.php '); $e = new Efficiencytester (); echo $e->test (' Rand ');?> At first I used Microtime () to get the time, and then I took into account that if I wanted to get a unit that was a second running time, it would not be polymorphic. Then I wrote a getTime () function to get the timestamps of different units, but it seems that the target function is running longer, probably because the judgment in the GetTime () function takes up a part of the time.