Home > PHP > Calculation Your PHP Processing Time Using Simple Counter

Calculation Your PHP Processing Time Using Simple Counter

October 27th, 2008 Leave a comment Go to comments

It’s a good idea for PHP progammer to know how fast their application process. With this info they can decide what best coding to achieve high perfomance. We’ll using microtime() function that’s already exist in PHP. Microtime will returns the current timestamp with microseconds.

The basic idea is when first PHP running the process, counting function will work and note as begin time and when process is finished counting function call again and note as end time. From this two variable we can get all processing time by decrease end time with begin time. The result will return in microseconds.

Now it’s full code. Place this code in first line after open tag PHP if possible ( be carefull with any session function).

$time = explode(' ', microtime());
$time = $time[1] + $time[0]; // return array
$begintime = $time; //define begin time

And place this code before closing tag PHP.

$time = explode(" ", microtime());
$time = $time[1] + $time[0];
$endtime = $time; //define end time
$totaltime = ($endtime - $begintime); //decrease to get total time
echo $totaltime.' seconds'; //echo it to appear in browser

Now anytime your PHP running it will return how long the process took :)

Categories: PHP Tags:
  1. francis
    February 17th, 2009 at 20:24 | #1

    This works flawlessly… Question how do i reduce the decimal positions, round it to lest say:

    0.0041

    instead of

    0.0040590763092

    Fantastic!@

  2. Admin
    March 2nd, 2009 at 22:34 | #2

    Of course you can . just use round() function .. like this ….

    $totaltime = round($endtime – $begintime, 4) //it’s will round to 4 digit in decimal.

    regards :)

  3. September 3rd, 2009 at 17:11 | #3

    Excellent site, keep up the good work

  4. January 30th, 2010 at 09:24 | #4

    bravo!

  1. April 24th, 2009 at 02:05 | #1