How does php calculate time of execution?

microtime(true) with the parameter true will return a float number in seconds, the number of seconds has passed since the Unix epoch (0:00:00 January 1,1970 GMT). Call this function before and after a code snippet, and the differences of the two float numbers is the running time for that code snippet in seconds.

<?php
$start_time = microtime(true);

for($i=0; $i<100000; $i++)
{
	echo $i;
}

$end_time = microtime(true);
echo "\n Start time: $start_time\n End Time: $end_time\n";
$executionTime = $end_time - $start_time;
echo "\n Time taken: $executionTime seconds.\n";

Search within Codexpedia

How does php calculate time of execution?

Custom Search

Search the entire web

How does php calculate time of execution?

Custom Search

  • About microtime() PHP Function
  • Find Code Execution Time

Reading Time: 4 minutes

1,288 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 –

  • Find and Count All Headings From a Web Page in PHP
  • Find and Extract All Headings From a Web Page in PHP
  • Find and Extract All Images From a Web Page in PHP
  • Find and Extract All links From a HTML String in PHP

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.

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.

How is execution time calculated?

1) Create a loop around whatneeds to be measured, that executes 10, 100, or 1000 times or more. Measure execution time to the nearest 10 msec. Then divide that time bythe number of times the loop executed. If the loop executed 1000 timesusing a 10 msec clock, you obtain a resolution of 10 µsec for theloop.

What is PHP execution time?

One important aspect of PHP programs is that the maximum time taken to execute a script is 30 seconds. The time limit varies depending on the hosting companies but the maximum execution time is between 30 to 60 seconds.

How are PHP programs executed?

Basically, each time a PHP script is loaded, it goes by two steps :.
The PHP source code is parsed, and converted to what's called opcodes. Kind of an equivalent of JAVA's bytecode. If you want to see what those look like, you can use the VLD extension..
Then, those opcode are executed..

What is the default execution time set in Set_time_limit () in PHP?

The default limit is 30 seconds or, if it exists, the max_execution_time value defined in the php. ini . When called, set_time_limit() restarts the timeout counter from zero.