Php obtains the execution time of the target function. Write a class to test the execution time of the target function. The following is the definition code of the class: copy the Code as follows :? Php *** classEfficiencyTester ** Efficiency tester. The test function writes a class to test the execution time of the target function. The following is the class definition code:
The Code is as follows:
/**
* Class EfficiencyTester
* Efficiency tester: test the function running time
* @ Version 1.0 2013.04.13
* @ Author Kross
*/
Class EfficiencyTester {
/**
* Var $ testTimes
* Number of tests
*/
Private $ testTimes = 1000;
/**
* Function getTime ()
* Obtain the timestamp based on the time mode.
* @ Param $ timeModel time mode. Default Value: microsecond
* @ 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 Value: microsecond
* @ Return double the time when 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 ()
* If the target function is tested multiple times, the running time (average value) is returned)
* @ Param $ functionName target function name
* @ Param $ timeModel time mode. Default Value: microsecond
* @ Return double the running time of the target function
*/
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 of the class:
The Code is as follows:
Require_once ('../class/EfficiencyTester. class. php ');
$ E = new EfficiencyTester ();
Echo $ e-> test ('rand ');
?>
At first, I directly used microtime () to get the time. Later I thought that if I wanted to get the running time in seconds, the write would not be polymorphism enough. Then I wrote a getTime () but it seems that the running time of the target function is longer, probably because the judgment in the getTime () function takes some time.
Bytes. The following code defines the class :? Php/*** class EfficiencyTester * Efficiency tester to test functions...