Thursday, October 23, 2025
HomeLanguagesMeasuring script execution time in PHP

Measuring script execution time in PHP

Script execution time in PHP is the time required to execute the PHP script. To calculate script execution time use clock time rather than the CPU execution time. Knowing clock time before script execution and after script execution will help to know the script execution time.

Example: Sample script




<?php
for($i = 1; $i <=1000; $i++)
{
    echo "Hello Geeks!";
} 
?>


Clock time can get using microtime() function. First use it before starts the script and then at the end of the script. Then using formula (End_time – Start_time). The microtime() function returns time in seconds. Execution time is not fixed, it depends upon the processor.

Example:




<?php
  
// Starting clock time in seconds
$start_time = microtime(true);
$a=1;
  
// Start loop
for($i = 1; $i <=1000; $i++)
{
    $a++;
} 
  
// End clock time in seconds
$end_time = microtime(true);
  
// Calculate script execution time
$execution_time = ($end_time - $start_time);
  
echo " Execution time of script = ".$execution_time." sec";
?>


Output:

Execution time of script = 1.6927719116211E-5 sec
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS