<?php<br />/*<br /> * This class enables you to mark points and calculate the time difference<br /> * between them. Memory consumption can also be displayed.<br /> *<br /> * @packageCodeIgniter<br /> * @subpackageLibraries<br /> * @categoryLibraries<br /> * @authorExpressionEngine Dev Team<br /> * @linkhttp://codeigniter.com/user_guide/libraries/benchmark.html<br /> */<br />class CI_Benchmark {</p><p>var $marker = array();</p><p>// --------------------------------------------------------------------</p><p>/**<br /> * Set a benchmark marker<br /> *<br /> * Multiple calls to this function can be made so that several<br /> * execution points can be timed<br /> *<br /> * @accesspublic<br /> * @paramstring$namename of the marker<br /> * @returnvoid<br /> */<br />function mark($name)<br />{<br />$this->marker[$name] = microtime();<br />}</p><p>// --------------------------------------------------------------------</p><p>/**<br /> * Calculates the time difference between two marked points.<br /> *<br /> * If the first parameter is empty this function instead returns the<br /> * {elapsed_time} pseudo-variable. This permits the full system<br /> * execution time to be shown in a template. The output class will<br /> * swap the real value for this variable.<br /> *<br /> * @accesspublic<br /> * @paramstringa particular marked point<br /> * @paramstringa particular marked point<br /> * @paramintegerthe number of decimal places<br /> * @returnmixed<br /> */<br />function elapsed_time($point1 = '', $point2 = '', $decimals = 4)<br />{<br />if ($point1 == '')<br />{<br />return '{elapsed_time}';<br />}</p><p>if ( ! isset($this->marker[$point1]))<br />{<br />return '';<br />}</p><p>if ( ! isset($this->marker[$point2]))<br />{<br />$this->marker[$point2] = microtime();<br />}</p><p>list($sm, $ss) = explode(' ', $this->marker[$point1]);<br />list($em, $es) = explode(' ', $this->marker[$point2]);</p><p>return number_format(($em + $es) - ($sm + $ss), $decimals);<br />}</p><p>// --------------------------------------------------------------------</p><p>/**<br /> * Memory Usage<br /> *<br /> * This function returns the {memory_usage} pseudo-variable.<br /> * This permits it to be put it anywhere in a template<br /> * without the memory being calculated until the end.<br /> * The output class will swap the real value for this variable.<br /> *<br /> * @accesspublic<br /> * @returnstring<br /> */<br />function memory_usage()<br />{<br />return '{memory_usage}';<br />}</p><p>}</p><p>// END CI_Benchmark class</p><p>/* End of file Benchmark.php */<br />/* Location: ./system/libraries/Benchmark.php */<br />
一個小例子,如下:
<?php</p><p>header("Content-Type:text/html;charset=UTF-8");<br />if( ! defined('BASEPATH') ) {<br />define ('BASEPATH', $_SERVER['DOCUMENT_ROOT'].'/news/');<br />}<br />require_once BASEPATH.'service/Json_service.php';<br />require_once BASEPATH.'common/Benchmark.php';</p><p>$benchmark = new CI_Benchmark();<br />$benchmark->mark('10');<br />$benchmark->mark('11');</p><p>$benchmark->mark('12');<br />$jsonservice = new JsonService();<br />$benchmark->mark('13');<br />$resultjson = $jsonservice->getNewsTitleJsonArray($categoryid=143746653,$index=1,$max=20,$lastpage,$lastid);<br />$benchmark->mark('14');<br />$jsonservice->clear();<br />//$size = strlen($resultjson);<br />//header("Content-Length: $size");<br />$benchmark->mark('15');<br />echo '-----------------------------------------'.'<br>';<br />echo $benchmark->elapsed_time('10','11',6).'<br>';<br />echo $benchmark->elapsed_time('11','12',6).'<br>';<br />echo $benchmark->elapsed_time('12','13',6).'<br>';<br />echo $benchmark->elapsed_time('13','14',6).'<br>';<br />echo $benchmark->elapsed_time('14','15',6).'<br>';<br />?>