Learn how to create and register your own WordPress shortcodes to add dynamic content to your posts and pages.
Measuring script/code execution time in PHP, microtime
If you haven’t measured the execution time of your PHP scripts, then now is the time to start measuring the execution time of your PHP code. As you optimise your code it should become better structured, more concise and cleaner.
Code performance is a critical consideration for any application. In general, software can live or die based on performance. Take PageSpeed Insights, for example. Google cares about the execution speed of our code, and so should you.
If we want to measure the execution time of a script, or code snippet, all we need to do is subtract the time the script started from the time the script ended. Once we know the answer to this sum then we can store it for analysis. This is principally the same for every programming language, from PHP to C#.
There are a few functions in PHP we could use to get the current time. However, for most use cases you probably want to use microtime.
Get the current time in PHP
As mentioned already, to measure execution time we need to be able to fetch the current time. To measure execution time in PHP we are going to use a function called microtime. Microtime is a PHP timestamp function to return the current Unix timestamp in microseconds.
$currentTime = microtime(); echo $currentTime; // output: 0.29466300 1678110820
By default, microtime returns a string with the microseconds elapsed since the last second first, and the seconds since the Unix Epoch second. Handily for us, microtime accepts a boolean parameter named ‘as_float’. By Passing ‘true’ to the as_float parameter, microtime will return a float representing the current time in seconds.
$as_float = true; $currentTime = microtime($as_float); echo $currentTime; // output: 1678110820.2947
As you can see, this gives us a much better metric to use for our subtraction.
It is worth noting that you could use other PHP time functions to achieve similar results, such as ‘time’. Similarly to microtime, time will return the current time since the Unix Epoch, but measured in seconds. This generally isn’t as useful for measuring execution time because it is less precise.
How to measure script execution time in PHP
In order to measure the execution time of your PHP scripts we are going to get the time twice (using microtime), once at the start of the code we are measuring ($start), and once at the end ($end). Once we have these two variables we are going to subtract the start from the end ($end – $start).
// Grab the start time $start = microtime(true); // Do some super awesome, well optimised programmer code nonsense // Grab the end time $end = microtime(true); // Subtract the start from the end $elapsed = $end - $start; echo "Script executed in $elapsed seconds";
Hopefully you are now empowered with the knowledge to measure your PHP script execution time and ultimately optimise your code. Go forth and optimise some bottlenecks!
You might like:
Learn the difference between implements and extends in TypeScript. Use Implements to implement interfaces and types, and extends to inherit from classes.
In this tutorial we will look at using YAML in PHP. Learn about Parsing and Writing YAML files using Symfony's YAML component.
Find bottlenecks, optimise and clean your code, and speed up your apps by measuring the execution time of your PHP scripts using microtime.
Learn how to regenerate and update WordPress media and image sizes both programmatically (without plugin), and also with a handy plugin.
Ever seen constants like __DIR__ and __FILE__ being used in PHP? These are 'Magic Constants', and this is how we can use them.