PHP Get Target function Execution Time example _php tutorial

Source: Internet
Author: User
A class was written to test the execution time of the target function. The following is the definition code for the class:

Copy the Code code as follows:
/**
* 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 ()
* Based on time mode, get timestamp
* @param $timeModel time mode, default: microseconds
* @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 target function Once, return run time
* @param $functionName The name of the target function
* @param $timeModel time mode, default: microseconds
* @return double the time the target function runs once (very randomly)
*/
Public Function Testonce ($functionName, $timeModel = ' MS ') {
$startMicroTime = $this->gettime ($timeModel);
$functionName ();
$endMicroTime = $this->gettime ($timeModel);

$costMicroTime = $endMicroTime-$startMicroTime;

return $costMicroTime;
}
/**
* Function Test ()
* Test target function multiple times, return run time (average)
* @param $functionName The name of the target function
* @param $timeModel time mode, default: microseconds
* @return double the time the 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 the Code code as follows:
Require_once ('.. /class/efficiencytester.class.php ');
$e = new Efficiencytester ();
echo $e->test (' Rand ');
?>

At first I was directly using Microtime () to get the time, then consider that if you want to get the unit is the second run time, so write is not enough polymorphism, and then I wrote a gettime () function to get different units of time stamp, but in this way, it seems that the objective function of the run time has become longer, This may be because the judgment in the GetTime () function takes a part of the time.

http://www.bkjia.com/PHPjc/736812.html www.bkjia.com true http://www.bkjia.com/PHPjc/736812.html techarticle A class was written to test the execution time of the target function. The following is the definition code for the class: the copy code code is as follows: PHP/** * Class Efficiencytester * efficiency tester, test function ...

  • 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.