Simple benchmarks in PHP

If you’re doing some basic profiling of your PHP scripts, the build-in microtime function can help you make some simple benchmarking fast. Here’s a rough example to show you how it could be used. The doSomething function is the function we want to benchmark.

1
2
3
4
5
6
7
8
9
10
11
12
13
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$start = $time;
 
doSomething();
 
$time = microtime();
$time = explode(' ', $time);
$time = $time[1] + $time[0];
$finish = $time;
$total_time = round(($finish - $start), 6);
if ($debug) print ("<p>Processing took approximately $total_time seconds</p>");

The above example is pretty rough. If you want to see a slightly more complete benchmark example, I do have it available (with source too).