Php Microtime

This is a handy bit of code that will help when optimizing your script. Most MVC or NonMVC frameworks have a benchmark feature but if you are on your own this will do just fine.

The PHP microtime() function will return the current Unix timestamp.

There isn’t much to this script, we are going to assign $time-start the start time. Then below your code to time place the $time_end.

A little simple math will tell us the difference. $time-start minus $time-end will give us the execution time.

If we were to use the raw result we would see something like this: 0.0562338829041 Seconds.

Its a bit to detailed do be of any use. We can use the PHP Function round() to round the number to 3 decimal places.

$time_start = microtime(true);

// Code Here
for ( $counter = 1; $counter <= 1000000; $counter += 1) {

$time_end = microtime(true);
$time = $time_end - $time_start;

echo "Loaded in ".round($time, 3)." secondsn";

That’s it!

Signup for my mailing list

Receive other rambings like this on design, code, and some times food.