Php obtains the execution time of the target function.
- /**
- * 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;
- }
- }
- ?>
Test code:
- Require_once ('../class/EfficiencyTester. class. php ');
- $ E = new EfficiencyTester ();
- Echo $ e-> test ('Rand ');
- ?>
In the first place, we directly used microtime () to get the time. later, we thought that if we wanted to get the running time in seconds, the write would not be polymorphism enough, and then we wrote a getTime () to obtain timestamps of different units. It seems that the running time of the target function is longer, probably because the judgment in the getTime () function takes some time. |