PHP records the code running time

Source: Internet
Author: User
PHP records code running time, code running time measurement

Generally, the test code is added to the code that requires performance for calculation.

However, every time you write microtime, end-start may not be too troublesome, so you simply write a class.

Code
class TimeCost{    private $cost = array();    private $record = array();    private $scale = 6;    public function __construct($scale = 6)    {        $this->cost = array();        $this->record = array();        $this->scale = $scale;    }    public function __toString()    {        return  $this->getString();    }    /**     * start to cal time.     *     * @param mixed $key     */    public function addCost($key)    {        $this->cost[$key] = microtime(true);    }    /**     * stop to cal time.     *     * @param mixed $key     */    public function closeCost($key)    {        $cost  = bcsub(microtime(true), $this->cost[$key], $this->scale);        if (in_array($key, array_keys($this->record))) {            $this->record[$key] = bcadd($cost, $this->record[$key], $this->scale);        } else {            $this->record[$key] = $cost;        }        return  $cost;    }        public function getString($key = null)    {        if ($key) {            return  "{$key}[{$this->record[$key]}]";        }        $str = '';        foreach ($this->record as $k => $v) {            $str .= "{$k}[{$v}]";        }        return  $str;    }}
Usage
$obj = new TimeCost();$token = 'test_a';$obj->addCost($token);some_code();$obj->closeCost($token);$reslut = $obj->getString($token);
Description
  • Time precision: the default value is 6 bits, which is enough. to achieve higher precision, you can specify the $ scale parameter when creating a new object.

  • Token: token is used to indicate a piece of code. the corresponding result is written into the record array in the form of key (token) and value.

    Therefore, the results of addCost and closeClost are accumulated multiple times with a token.

  • GetString: if the token is passed, the result corresponding to the token is returned. by default, all results in the record are spliced and returned.

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.