PHP code execution time

后端 未结 9 993
既然无缘
既然无缘 2021-02-14 14:13

How to find my code execution time?

I am using the below snippet.

I am not happy with that?

list ($msec, $sec) = explode(\' \', microtime());
$mi         


        
9条回答
  •  故里飘歌
    2021-02-14 14:37

    I have created this simple class for that:

    class timer
    {
        private $start_time = NULL;
        private $end_time = NULL;
    
        private function getmicrotime()
        {
          list($usec, $sec) = explode(" ", microtime());
          return ((float)$usec + (float)$sec);
        }
    
        function start()
        {
          $this->start_time = $this->getmicrotime();
        }
    
        function stop()
        {
          $this->end_time = $this->getmicrotime();
        }
    
        function result()
        {
            if (is_null($this->start_time))
            {
                exit('Timer: start method not called !');
                return false;
            }
            else if (is_null($this->end_time))
            {
                exit('Timer: stop method not called !');
                return false;
            }
    
            return round(($this->end_time - $this->start_time), 4);
        }
    
        # an alias of result function
        function time()
        {
            $this->result();
        }
    
    }
    

    To time scripts, you can put it to use like:

    $timer = new timer();
    
    $timer->start();
    // your code now
    $timer->stop();
    
    // show result now
    echo $timer->result();
    

提交回复
热议问题