27

I have a large PHP code in my website, I want to know the execution time of processing. How can I do this?

<?php
// large code
// large code
// large code

// print execution time here
?>
  • Related: [Loading time of php page in apache server](http://stackoverflow.com/questions/13411389/loading-time-of-php-page-in-apache-server) – Ja͢ck Jun 11 '13 at 03:08

1 Answers1

92

You can use microtime as the start and end of your PHP code:

<?php
    $time_start = microtime(true);
    sleep(1);
    $time_end = microtime(true);
    $time = $time_end - $time_start;
    echo "Process Time: {$time}";
    // Process Time: 1.0000340938568
?>

As of PHP 5.4.0, there is no need to get start time at the beginning, the $_SERVER superglobal array already has it:

<?php
    sleep(1);
    $time = microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"];
    echo "Process Time: {$time}";
    // Process Time: 1.0061590671539
?>