Example of php getting the execution time of the target function _ PHP Tutorial

Source: Internet
Author: User
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...

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.