10

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;

9 Answers 9

21

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();
Sign up to request clarification or add additional context in comments.

Comments

9

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.

Comments

5

You can use the parameter of microtime :

$microtime = microtime(true);

Comments

3

If you want to profile your script, use XDebug + {K,Win}Cachegrind, these are free tools that provide a visual map of your code's execution.

2 Comments

+1 for suggesting a profiler instead of using microtime(), see xdebug.org/docs/profiler
There is useful function xdebug_time_index it is show elapsed time since start of the script
2

If you want the actual processor timings:

$rUsage = getrusage();
echo 'User time = '.sprintf('%.4f',($rUsage['ru_utime.tv_sec'] * 1e6 + $rUsage['ru_utime.tv_usec']) / 1e6).' seconds';
echo 'system time = '.sprintf('%.4f',($rUsage['ru_stime.tv_sec'] * 1e6 + $rUsage['ru_stime.tv_usec']) / 1e6).' seconds';

Comments

1

In LINUX:

For one simple code:

time php -r "echo 'hello';"

output:

hello
real 0m0.095s
user 0m0.033s
sys 0m0.055s

For a php file:

time php hello.php

output:

hello
real 0m0.073s
user 0m0.025s
sys 0m0.047s

Comments

0

Thanks for sharing your information.

The below one also LITTLE BIT near of my solution.

function microtime_float() {
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

$time_start = microtime_float();

SOURCE CODE

$time_end = microtime_float();
$time = round($time_end - $time_start, 4);

echo "Last uncached content render took $time seconds";

Comments

0

If running Unix, take a look at the time command:

Comments

0
<?php
// Randomize sleeping time
usleep(mt_rand(100, 10000));

// As of PHP 5.4.0, REQUEST_TIME_FLOAT is available in the $_SERVER superglobal array.
// It contains the timestamp of the start of the request with microsecond precision.
$time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];

echo "Did nothing in $time seconds\n";
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.