CI Framework Learning Note 4--benchmark.php

Source: Internet
Author: User

We went back to codeigniter.php and looked down, the first class file introduced is benchmark.php, which is primarily provided with benchmark, specific usage reference manuals http://codeigniter.org.cn/user_guide /libraries/benchmark.html. It is recommended that the small partners read the manual, understand the use of the function, and then analyze the code before it will do more with less. Not much to say, the following to get to the chase.

The test class defines an array variable public $marker = Array (), whose purpose is primarily to record the test points we add to the file.

 Public function mark ($name)    {        $this->marker[$namemicrotime (TRUE);    }

The content of the mark method is simple, one line of code, which records the current timestamp of the test point.

 Public functionElapsed_time ($point 1= ",$point 2= ",$decimals= 4)    {        if($point 1= = = "")        {            return' {elapsed_time} '; }        if( !isset($this->marker[$point 1]))        {            return‘‘; }        if( !isset($this->marker[$point 2]))        {            $this->marker[$point 2] =Microtime(TRUE); }        return Number_format($this->marker[$point 2] -$this->marker[$point 1],$decimals); }

The Elapsed_time method has three parameters, $point 1 and $point2 are the test point names that calculate the time difference, $decimals is the exact number of digits.

When $point1 is empty, the string {Elapsed_time} is returned, in fact the string is replaced by the output component.

$elapsed $BM->elapsed_time (' Total_execution_time_start ', ' total_execution_time_end '); $output Str_replace (arrayarray($elapsed$memory$output);

The above two lines are the code in the output component, and you can see that the difference between Total_execution_time_start and Total_execution_time_end is actually obtained, where total_execution_time _start is the first point in time after the frame is loaded, and total_execution_time_end is not defined in the frame and continues to look down until $POINT2 is defined as the current timestamp. So the time difference here is actually about how long it takes for the system to load and run to the difference.

Looking down the code is very simple, $point 1 empty when the return null value, $point 2 is null to take the test point 1 to the current time difference.

 Public function memory_usage ()    {        return ' {memory_usage} ';    }

The Memory_usage method is used to obtain the current memory usage.

The content of the benchmark.php file is simple and the complete code is as follows.

classCi_benchmark {/** * List of all benchmark markers * * @var array*/     Public $marker=Array(); /** Set A benchmark marker * * Multiple calls to this function can is made so that several * execution     Points can be timed. * * @param string $name Marker name * @return void*/     Public functionMark$name)    {        $this->marker[$name] =Microtime(TRUE); }    // --------------------------------------------------------------------    /** * Elapsed Time * * Calculates the time difference between, marked points. * * If The first parameter is empty this function instead returns the * {Elapsed_time} pseudo-variable. This permits the full system * Execution time to being shown in a template.     The output class would * swap the real value for this variable.  * * @param string $point 1 A particular marked point * @param string $point 2 a particular Marked point * @param int $decimals number of decimal places * * @return string calculated ELA Psed time on success, * a ' {elapsed_string} ' if $point 1 is the empty * or an empty string if $p     Oint1 is not found. */     Public functionElapsed_time ($point 1= ",$point 2= ",$decimals= 4)    {        if($point 1= = = "")        {            return' {elapsed_time} '; }        if( !isset($this->marker[$point 1]))        {            return‘‘; }        if( !isset($this->marker[$point 2]))        {            $this->marker[$point 2] =Microtime(TRUE); }        return Number_format($this->marker[$point 2] -$this->marker[$point 1],$decimals); }    // --------------------------------------------------------------------    /** * Memory Usage * * Simply returns the {memory_usage} marker.     * * This permits is put it anywhere in a template * without the memory being calculated until the end.     * The output class would swap the real value for this variable. * * @return string ' {memory_usage} '*/     Public functionMemory_usage () {return' {memory_usage} '; }}

CI Framework Learning Note 4--benchmark.php

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.