How To Calculate Code Execution Time in PHP

Share this Article
Reading Time: 4 minutes
1,287 Views

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.

Syntax

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

<?php

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

Output

Micro Time (in string): 0.05135300 1648263455

Example #2

<?php

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

Output

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.

Next,

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

<?php

// 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.

Output

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.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshare free for 1 month! Learn CakePHP 4, Laravel APIs Development, CodeIgniter 4, Node Js, etc into a depth level. Master the Coding Skills to Become an Expert in Web Development. So, Search your favourite course and enroll now. Click here to join.

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.

Was this post helpful?

Learn Web Development Courses Risk Free @ $5 only.
Web Development Courses @ $5