PHP Gets the target function execution time sample

Source: Internet
Author: User

  This article mainly introduces PHP to get the target function execution time example, the need friend can refer to the following

A class is written to test the execution time of the target function. The following is the definition code for the class:     code is as follows: <?php/**  * class Efficiencytester  * efficiency tester, test function run time  * @version 1.0 201 3.04.13  * @author Kross  */class Efficiencytester {       /**      * var $testTimes      * test times        */    Private $testTimes = 1000;      /**      * function getTime ()      * get timestamp      * according to time mode @param $timeModel time mode, default: microsecond      * @return int timestamp      */    Private function GetTime ($timeModel = ' ms ') {        if ($timeModel = = ' ms ') {          &NBS P return Microtime ();        } else if ($timeModel = = ' S ') {            return time () &nbsp ;      } else {            return microtime ();       &nbsp {   }    /**      * function testonce ()      * test objective function once, return to run time      * @param $functionName target function name      * @param $timeModel time mode, default: microsecond      * @retur N Double objective function time (very random)      */    Public Function testonce ($functionName, $timeModel = ' MS ') {                $startMicroTime = $this->gettime ($timeModel);     & nbsp   $functionName ();         $endMicroTime = $this->gettime ($timeModel);           $costMicroTime = $endMicroTime-$startMicroTime;           return $costMicroTime;    }    /**     * function test ()     * Testing the target function multiple times, return to run time (average)     * @pa Ram $functionName target function name     * @param $timeModel time mode, default: microseconds     * @return Double objective functions time of the operation   &NBSP ; * *   &NBsp Public function test ($functionName, $timeModel = ' MS ') {        $totalMicroTimes = 0;     & nbsp   for ($i = 1; $i <= $this->testtimes; $i + +) {            $totalMicroTimes + = $t His->testonce ($functionName);        }         return $totalMicroTimes/$this->testtimes;    }?>       The following is the test code for the class: the     code is as follows: <?php require_once (' ... /class/efficiencytester.class.php '); $e = new Efficiencytester (); echo $e->test (' Rand ');?>     At first I used Microtime () to get the time, and then I took into account that if I wanted to get a unit that was a second running time, it would not be polymorphic. Then I wrote a getTime () function to get the timestamps of different units, but it seems that the target function is running longer, probably because the judgment in the GetTime () function takes up a part of the time.
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.