PHP Get Target function Execution Time example _php instance

Source: Internet
Author: User

A class is written to test the execution time of the target function. The following is the definition code for the class:

Copy Code code as follows:

<?php
/**
* 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 ()
* Get time stamp based on time pattern
* @param $timeModel time mode, default: microsecond
* @return int time stamp
*/
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, return to run time
* @param $functionName The name of the target function
* @param $timeModel time mode, default: microsecond
* @return double the time 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 ()
* Test the target function multiple times, return run time (average)
* @param $functionName The name of the target function
* @param $timeModel time mode, default: microsecond
* @return The time the double 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 Code code as follows:

<?php
Require_once ('.. /class/efficiencytester.class.php ');
$e = new Efficiencytester ();
echo $e->test (' Rand ');
?>

At first I was using microtime () to get the time, then consider if you want to get the unit is seconds of running time, so write is not polymorphic, and then I wrote a gettime () function to get different units of time stamps, but so, seemingly the target function of the running time is longer, It may be that the judgment in the GetTime () function takes up a part of the 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.