How To Calculate Code Execution Time in PHP

Reading Time: 4 minutes

Inside this article we will see how to calculate code execution time in php. In every application development Page load, Application response, etc to end users are the most important factor for successful release.

This tutorial contains a classified information about finding the total time by a code to execute. This is a very simple process, once you learn I hope you will never forget the concept.

In PHP language we have a function to start and stop a timer i.e microtime(). The microtime() function is an inbuilt function in PHP which is used to return the current Unix timestamp with microseconds.

We will use the concept of microtime() function to find the code execution time.

Learn More –

Let’s get started.

About microtime() PHP Function

The microtime() function is an inbuilt function in PHP which is used to return the current Unix timestamp with microseconds.


microtime() or microtime(false)

Above function returns the micro time value in string. By default, it returns the value in string. But when we pass true value into it, it returns the value into float.

Example #1


echo "Micro Time (in string): ".microtime();


Micro Time (in string): 0.05135300 1648263455

Example #2


echo "Micro Time (in float): ".microtime(true);


Micro Time (in float): 1648263991.9188

We can clearly see the difference in return values – String & Float. We will use the concept of getting code execution time using this function and we use this float return type.

Find Code Execution Time

Let’s create a sample PHP application which simply runs a for loop which starts from index 0 to index 100000000. We will see how much time it will take to run this entire loop.


Create a file index.php inside localhost directory. Open file and write this code into it. Code is pretty simple, you will understand easily.


// starting time
$time_start = microtime(true);

// Code of program
$num = 0;

for ($i = 0; $i < 100000000; $i = $i + 1) {
    $num += 10;

// ending time
$time_end = microtime(true);

// Time difference
$time = $time_end - $time_start;

echo "Code Execution Time: " . $time;

When we execute this application. The return value will be in unix micro seconds.


Code Execution Time: 1.8178100585938

By the using the same concept you can calculate your PHP code execution time as well as MySQL queries time.

We hope this article helped you to learn How To Calculate Code Execution Time in PHP Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more