How to show script execution time in PHP (one line)

This post shows you how to get page (script) execution time in PHP, using one line of code.

There are two methods of displaying the page generation time in PHP.

Old PHP versions can do this as below:

$time_start = microtime(true);
// Write your code here…
$time_end = microtime(true);
$gen_time = $time_end – $time_start;
echo “This page has been generated in: {$gen_time} seconds.”;

As of PHP 5.4.0 you can simply measure page generation time as follows (append this at the end of your php script file):

$gen_time = microtime(true) – $_SERVER[“REQUEST_TIME_FLOAT”];
echo “This page has been generated in: {$gen_time} seconds.”;

If you need a reliable Dedicated Server or VPS, you should definitely try InterServer Hosting. You can test InterServer for 1 Penny, for the first month, using this coupon code: WSG1PENNY.