A Web Design Community curated by Chris Coyier

Code Snippets Gallery

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);

4 Responses

  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. */

  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 :)

Leave a Comment

Remember:
  • Be nice.
  • Wrap multiline code in <pre> and <code> tags and escape it first (turn <'s into &lt;'s).
  • You may use regular HTML stuff like <a href="">, <em>, and <strong>
* This website may or may not contain any actual CSS or Tricks.