Benchmarking your script

There is a few ways you can benchmark your php script. One of those is measuring the time between function calls, one of the most used, the other is measure the used memory by the script, and sometimes…both specially if you want to know everything about your code and how to make it faster and lighter.

For measure the time I use microtime(true); to get the the current current Unix timestamp with microseconds.

$start = microtime(true);
the_function_to_test();
$end = microtime(true);
$seconds = round($time_end - $time_start, 4);

So now we’ve the time used by our script, what about the memory?

$memory = round(memory_get_usage()/1024/1024, 2);
echo "Used memory: $memory MB";

And we’ve the memory peak too:

$memory = round(memory_get_peak_usage()/1024/1024, 2);
echo "Memory peak: $memory MB";

So we’ve all the tools we can to make our script faster, the next depend on us, make it happen.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *