• 4
name Punditsdkoslkdosdkoskdo

Accurate way to quantify execution times of php contents

I want to know how many milliseconds a PHP for-loop takes to execute.

I know the structure of a generic algorithm, but no idea how to implement it in PHP:

init1 = timer(); // where timer() is the amount of milliseconds from midnight
the loop begin
some code
the loop end
total = timer() - init1;

Create file loadtime.php

class loadTime{
    private $time_start     =   0;
    private $time_end       =   0;
    private $time           =   0;
    public function __construct(){
        $this->time_start= microtime(true);
    public function __destruct(){
        $this->time_end = microtime(true);
        $this->time = $this->time_end - $this->time_start;
        echo "Loaded in $this->time seconds

Than in the beggining of your script, after <?php write include 'loadtime.php'; $loadtime=new loadTime();

When page is loaded at the end there will be written "Loaded in x seconds"

  • 0
Reply Report

You can use microtime(true) with following manners:

Put this at the start of your php file:

//place this before any script you want to calculate time
$time_start = microtime(true);

// your script code goes here

// do something

Put this at the end of your php file:

// Display Script End time
$time_end = microtime(true);

//dividing with 60 will give the execution time in minutes other wise seconds
$execution_time = ($time_end - $time_start)/60;

//execution time of the script
echo '<b>Total Execution Time:</b> '.$execution_time.' Mins';
  • 0
Reply Report

You can use the microtime function for this. From the documentation:

microtime — Return current Unix timestamp with microseconds

If get_as_float is set to TRUE, then microtime() returns a float, which represents the current time in seconds since the Unix epoch accurate to the nearest microsecond.

Example usage:

$start = microtime(true);
while (...) {

$time_elapsed_secs = microtime(true) - $start;
  • 0
Reply Report

Related Questions

Trending Tags