Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP code execution time

Tags:

php

How to find my code execution time?

I am using the below snippet.

I am not happy with that?

list ($msec, $sec) = explode(' ', microtime());
$microtime = (float)$msec + (float)$sec;
like image 474
Bharanikumar Avatar asked May 31 '10 08:05

Bharanikumar


People also ask

How does PHP calculate code execution time?

Clock time can get using microtime() function. First use it before starts the script and then at the end of the script. Then using formula (End_time – Start_time). The microtime() function returns time in seconds.

What is PHP execution time?

One important aspect of PHP programs is that the maximum time taken to execute a script is 30 seconds. The time limit varies depending on the hosting companies but the maximum execution time is between 30 to 60 seconds.

Does PHP have a timeout?

The default timeout is 30 seconds. It can be changed using the max_execution_time php. ini directive or the corresponding php_value max_execution_time Apache httpd.

What does set_time_limit 0 mean?

The purpose of this function is to set your PHP script execution time to a certain set period of time. The default is normally 30 seconds, but check your php. ini file to find out the default. Some developers set this to 0 which means the script execution time will not have a time limit imposed.


2 Answers

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();
like image 147
Sarfraz Avatar answered Oct 13 '22 04:10

Sarfraz


I'm using following class to determine time elapsed:

class StopWatch {
    private static $total;

    public static function start() {
        self::$total = microtime(true);
    }

    public static function elapsed() {
        return microtime(true) - self::$total;
    }
}

You just have to call StopWatch::start at the beginning and StopWatch::elapsed whenever you want to know how much time elapsed from the start.

like image 41
Ondrej Slinták Avatar answered Oct 13 '22 03:10

Ondrej Slinták