Snippet Code To Benchmark Your PHP Script

Testing is one of the common tasks for a programmer. In every development phase, testing needs to be done before moving to the next phase. There are a lot of testing techniques. One of them is benchmarking, to test script/code performance. Benchmark test results can be used to optimize the code/script.

In PHP you can get how long your server took to parse and execute your code. Here is the simple snippet code to get the execution time:

$start = microtime(true);
// Script you want to test here
usleep(200000);  // sleep 2 seconds
$end = microtime(true);
$r = round($end - $start,4);
echo '<strong>Execution Time</strong>: '.$r.' seconds<br />';

This simple code will help you discover bottlenecks in your script — there are a load of things you can do to optimize your code/script; and I find that using functions as above don’t add extra load to your script.