Contact!

Measuring script/code execution time in PHP, microtime

Measuring script/code execution time in PHP using 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!

Join the discussion!

You might like:

Create, register and use shortcodes in WordPress

Create, register and use shortcodes in WordPress

by Gav 18/03/2023

Learn how to create and register your own WordPress shortcodes to add dynamic content to your posts and pages.

How to use guard clauses in JavaScript

How to/why use guard clauses in JavaScript

by Gav 16/03/2023

Learn how to improve code readability and performance by using guard clauses in JavaScript. Discover their benefits and best practices.

Implements and Extends, Object Oriented TypeScript

Implements and Extends, Object Oriented TypeScript

by Gav 15/03/2023

Learn the difference between implements and extends in TypeScript. Use Implements to implement interfaces and types, and extends to inherit from classes.

Reading/Parsing and Writing YAML files in PHP, Symfony

Reading/Parsing and Writing YAML files, PHP Symfony

by Gav 14/03/2023

In this tutorial we will look at using YAML in PHP. Learn about Parsing and Writing YAML files using Symfony's YAML component.

Measuring code execution performance in JavaScript

Measuring code execution performance in JavaScript

by Gav 13/03/2023

Measuring code execution performance is an important way to identify bottlenecks. Use these methods in JavaScript to help optimise your code.

Measuring script/code execution time in PHP using microtime

Measuring script/code execution time in PHP, microtime

by Gav 06/03/2023

Find bottlenecks, optimise and clean your code, and speed up your apps by measuring the execution time of your PHP scripts using microtime.

Regenerate WordPress media image sizes, programmatically

Regenerate WordPress media image sizes, programmatically

by Gav 25/02/2021

Learn how to regenerate and update WordPress media and image sizes both programmatically (without plugin), and also with a handy plugin.

Magic Constants in PHP. What they are and how to use them.

Magic Constants in PHP. What they are and how to use them

by Gav 15/02/2021

Ever seen constants like __DIR__ and __FILE__ being used in PHP? These are 'Magic Constants', and this is how we can use them.

Detecting Keypress JavaScript

Detect single and multiple keypress events: JavaScript

by Gav 16/10/2019

Learn how to use event listeners to detect and handle single and multiple keypress events in JavaScript. Add modifier keys to your application!