Blog

Best way to measure execution time for php script

Hello everyone,

I am writing this post that is very interesting and useful for developers that run their php script using cron or linux command line interface.

Generally it helps when we run our script that scrape or data mining  the data from remote website and we need calculate exact execution time of our script that took for execute, it will helpful to measure how many data is executed by our script.

Okay so lets start, I am assuming that you have a script that run using php and you have dataset in a veriable ,

/*Assume a veribale that have large dataset in object or array */

$myObject;

/*Loop started here */

/* initialize the time */

$initTime = microtime(true);

foreach($myObject as $key=>$value){

// Do  your business logic here

}

/* Again initialize the end time*/

$endTime = microtime(true);

$finalExecutionTime = ($initTime - $endTime)/60;

echo 'Script Took Time: '.$finalExecutionTime.' Mins'.PHP_EOL;

That’s it.

Thank you so much read the article.

Leave a Reply

Your email address will not be published. Required fields are marked *