Example of getting target function execution time in php_PHP tutorial

WBOY
Release: 2016-07-13 10:37:03
Original
835 people have browsed it

Written a class to test the execution time of the target function. The following is the definition code of the class:

Copy code The code is as follows:

/**
* class EfficiencyTester
* Efficiency tester, test function running time
* @version 1.0 2013.04.13
* @author Kross
*/
class EfficiencyTester {
/**
* var $testTimes
* Number of tests
*/
private $testTimes = 1000;

/**
* function getTime()
* Get the timestamp
according to the time mode * @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 the target function once and return the running time
* @param $functionName Target function name
* @param $timeModel Time mode, default: microseconds
* @return double The time it takes for the target function to run once (very random)
*/
public function testOnce($functionName, $timeModel = 'MS') { $startMicroTime = $this->getTime($timeModel);
$functionName();
        $endMicroTime = $this->getTime($timeModel);
$costMicroTime = $endMicroTime - $startMicroTime;

                                                                                                                            """""""""""""" > for ($i = 1; $i <= $this->testTimes; $i++) {

$totalMicroTimes += $this->testOnce($functionName);
}
return $totalMicroTimes / $this->testTimes;
}
}
?>



The following is the test code of the class:




Copy code

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, writing this way was not polymorphic enough, so I wrote a getTime() function to get it. Timestamps in different units, but in this way, it seems that the running time of the target function has become longer, possibly because the judgment in the getTime() function takes up part of the time.


http://www.bkjia.com/PHPjc/736812.html

www.bkjia.com

true
http: //www.bkjia.com/PHPjc/736812.html

TechArticle

Written a class to test the execution time of the target function. The following is the definition code of the class: Copy the code The code is as follows: ?php /** * class EfficiencyTester * Efficiency tester, test function...

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!