Example of php getting the execution time of the target function _ php instance

Source: Internet
Author: User
This article mainly introduces the example of php getting the execution time of the target function. If you need it, you can refer to 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.