A class is written to test the execution time of the target function. The following is the definition code for the class:
Copy Code code as follows:
<?php
/**
* 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 ()
* Get time stamp based on time pattern
* @param $timeModel time mode, default: microsecond
* @return int time stamp
*/
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, return to run time
* @param $functionName The name of the target function
* @param $timeModel time mode, default: microsecond
* @return double the time the target function runs once (very random)
*/
Public Function Testonce ($functionName, $timeModel = ' MS ') {
$startMicroTime = $this->gettime ($timeModel);
$functionName ();
$endMicroTime = $this->gettime ($timeModel);
$costMicroTime = $endMicroTime-$startMicroTime;
return $costMicroTime;
}
/**
* Function Test ()
* Test the target function multiple times, return run time (average)
* @param $functionName The name of the target function
* @param $timeModel time mode, default: microsecond
* @return The time the double 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 Code code as follows:
<?php
Require_once ('.. /class/efficiencytester.class.php ');
$e = new Efficiencytester ();
echo $e->test (' Rand ');
?>
At first I was using microtime () to get the time, then consider if you want to get the unit is seconds of running time, so write is not polymorphic, and then I wrote a gettime () function to get different units of time stamps, but so, seemingly the target function of the running time is longer, It may be that the judgment in the GetTime () function takes up a part of the time.