PHP : Find script execution time

If you are a PHP developer chances are many that you need to find how much time it took to execute a piece of code. Using the script below you can find how much time it took to execute your php code.


$time_start = microtime(true);

// execute your tasks here

$time_end = microtime(true);
$time = $time_end - $time_start;

echo "Code executed in $time seconds\n";

You can try these examples


$time_start = microtime(true);

// execute your tasks here
phpinfo();

$time_end = microtime(true);
$time = $time_end - $time_start;

echo "Code executed in $time seconds\n";

Another example


$time_start = microtime(true);

// execute your tasks here
sleep(1); // wait for 1 second

$time_end = microtime(true);
$time = $time_end - $time_start;

echo "Code executed in $time seconds\n";

ASP and C# version of the code can be found here

Check Javascript version here

This entry was posted in PHP and tagged . Bookmark the permalink.