Grow your CSS skills. Land your dream job.

Last updated on:

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

Comments

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

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

    • Matt
      Permalink to comment#

      Howdy,

      Just wanted to say thanks for this article, I used it to test the difference between a single MySQL call and a file being picked up locally. The difference was amazing Local file storage was 6-7 faster!!!!

      Matt

  3. Rafael
    Permalink to comment#

    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. Permalink to comment#

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

  5. I time all my programs and log the timing to a file to help me tune my database system. I have developed a class to help.

    &lt?php
    /**
     * Time Code Class
      * @author Russell Thompson
     */
    class pgmTiming
    {
          private $mtime;
          public function __construct()  {
              $this->mtime = microtime(true);
          }
          public function  __destruct()  {
               $pgmName = $_SERVER["SCRIPT_NAME"];
               print ("tm: " . $pgmName . " " . (microtime(true) -
                  this->mtime));
         }
    }

    in my programs, after I make sure the class is loaded (I depend on __autoload) All I need is one line.

    $tm = new pgmTiming();


    As part of php cleanup, the destructor will run and print out the timing. I have the instantiation statement in a require_once file.

    In the example above it just prints the result. My actual destructor writes to a log file so I can average timings.,

Leave a Comment

Posting Code

Markdown is supported in the comment area, so you can write inline code in backticks like `this` or multiline blocks of code in in triple backtick fences like ```this```. You don't need to escape code in backticks, Markdown does that for you.

Sadly, it's kind of broken. WordPress only accepts a subset of HTML in comments, which makes sense, because certainly some HTML can't be allowed, like <script> tags. But this stripping happens before the comment is processed by Markdown (via Jetpack). It seems to me that would be reversed, because after Markdown processes code in backticks, it's escaped, thus safe. If you think you can fix this issue, get in touch!

If you need to make sure the code (typically HTML) you post absolutely posts correctly, escape it and put it within <pre><code> tags.

Current ye@r *

*May or may not contain any actual "CSS" or "Tricks".