A class was written to test the execution time of the target function. The following is the definition code for the class:
Copy the Code code as follows:
/**
* Class Efficiencytester
* Efficiency tester, test function run time
* @version 1.0 2013.04.13
* @author Kross
*/
Class Efficiencytester {
/**
* Var $testTimes
* Number of tests
*/
Private $testTimes = 1000;
/**
* Function GetTime ()
* Based on time mode, get timestamp
* @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 target function Once, return run time
* @param $functionName The name of the target function
* @param $timeModel time mode, default: microseconds
* @return double the time the target function runs once (very randomly)
*/
Public Function Testonce ($functionName, $timeModel = ' MS ') {
$startMicroTime = $this->gettime ($timeModel);
$functionName ();
$endMicroTime = $this->gettime ($timeModel);
$costMicroTime = $endMicroTime-$startMicroTime;
return $costMicroTime;
}
/**
* Function Test ()
* Test target function multiple times, return run time (average)
* @param $functionName The name of the target function
* @param $timeModel time mode, default: microseconds
* @return double the time the target function runs
*/
Public function test ($functionName, $timeModel = ' MS ') {
$totalMicroTimes = 0;
for ($i = 1; $i <= $this->testtimes; $i + +) {
$totalMicroTimes + = $this->testonce ($functionName);
}
Return $totalMicroTimes/$this->testtimes;
}
}
?>
The following is the test code for the class:
Copy the Code code as follows:
Require_once ('.. /class/efficiencytester.class.php ');
$e = new Efficiencytester ();
echo $e->test (' Rand ');
?>
At first I was directly using Microtime () to get the time, then consider that if you want to get the unit is the second run time, so write is not enough polymorphism, and then I wrote a gettime () function to get different units of time stamp, but in this way, it seems that the objective function of the run time has become longer, This may be because the judgment in the GetTime () function takes a part of the time.
http://www.bkjia.com/PHPjc/736812.html www.bkjia.com true http://www.bkjia.com/PHPjc/736812.html techarticle A class was written to test the execution time of the target function. The following is the definition code for the class: the copy code code is as follows: PHP/** * Class Efficiencytester * efficiency tester, test function ...