Home >Backend Development >PHP Tutorial >Example of getting target function execution time in php_PHP tutorial
Written a class to test the execution time of the target function. The following is the definition code of the class:
/**
* function getTime()
* Get the timestamp
according to the time mode * @param $timeModel time mode, default: microseconds
* @return int timestamp
*/
private function getTime($timeModel = 'MS') {
if ($timeModel == 'MS') {
return microtime();
} Else if ($ Timemodel == 's') {
Return Time ();
} Else {
Return Microtime ();
}
}
/**
* function testOnce()
* Test the target function once and return the running time
* @param $functionName Target function name
* @param $timeModel Time mode, default: microseconds
* @return double The time it takes for the target function to run once (very random)
*/
public function testOnce($functionName, $timeModel = 'MS') { $startMicroTime = $this->getTime($timeModel);
$functionName();
$endMicroTime = $this->getTime($timeModel);
$costMicroTime = $endMicroTime - $startMicroTime;
"""""""""""""" > for ($i = 1; $i <= $this->testTimes; $i++) {
$totalMicroTimes += $this->testOnce($functionName);
}
return $totalMicroTimes / $this->testTimes;
}
}
?>
The following is the test code of the class:
Copy code
The code is as follows:
$e = new EfficiencyTester();
echo $e->test('rand');?>
Written a class to test the execution time of the target function. The following is the definition code of the class: Copy the code The code is as follows: ?php /** * class EfficiencyTester * Efficiency tester, test function...