Calculation Your PHP Processing Time Using Simple Counter
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
This works flawlessly… Question how do i reduce the decimal positions, round it to lest say:
0.0041
instead of
0.0040590763092
Fantastic!@
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
Excellent site, keep up the good work
bravo!