A Web Design Community curated by Chris Coyier

A little dab'll do ya

Code Snippets

Home » Code Snippets » PHP » Count Script Excecution Time Submit one!

Count Script Excecution Time

$execution_time = microtime(); # Start counting

# Your code

$execution_time = microtime() - $execution_time;
$execution_time = sprintf('It took %.5f sec', $execution_time);

Subscribe to The Thread

  1. $execution_time = sprintf(‘It took %.5f sec’, $execution_time);
    echo $execution_time;

    # or

    printf(‘It took %.5f sec’, $execution_time);

    # to display the output.

  2. daGrevis says:

    This technique will not work correctly!
    I would suggest something like this…


    $time = microtime();
    $time = explode(' ', $time);
    $time = $time[1] + $time[0];
    $start = $time;

    // Your code.

    $time = microtime();
    $time = explode(' ', $time);
    $time = $time[1] + $time[0];
    $finish = $time;

    $page_time = ($finish - $start);
    $page_time = round($page_time, 3); // For users.

    echo $page_time;

    /* By daGrevis. */

    • EPD says:

      Here’s the stripped down version of daGrevis’ fine work:


      $startTime = array_sum(explode(' ', microtime()));

      /* Your code here */

      $totalTime = array_sum(explode(' ', microtime())) - $startTime;
      echo $totalTime;

  3. Rafael says:

    Since php 5.1, the timestamp of the start of the request is available in $_SERVER['REQUEST_TIME']

    So you can do :

    $page_time = round(microtime(true)-$_SERVER['REQUEST_TIME'], 3);

  4. Djkanna says:

    Perhaps “register_shutdown_function()” could be of some use here :)

It's Your Turn

At this moment, you have an awesome opportunity* to be the person your mother always wanted you to be: kind, helpful, and smart. Do that, and we'll give you a big ol' gold star for the day (literally).

Posting tips:
  • You can use basic HTML
  • When posting code, please turn all
    < characters into &lt;
  • If the code is multi-line, use
    <pre><code></code></pre>
Thank you,
--- The Management ---