PHP Code

How to record your script loading time with PHP

If you have a resource intensive PHP script – perhaps one that iterates through and processes a lot of records in a database on a regular basis – it's good to be able to track the script loading time, and then be able to monitor whether your tweaks to the code improve the process or make it longer.

Here's the quick code to track the loading time:

// place this at the top of your script
$time_start = time();

//place this at the bottom of your script, or after the loop that takes a long time
$time_end = time();
$time_lapse = $time_end - $time_start;
// mail admin message
$msg = "Update took $time_lapse seconds.\n"

In one instance, I divided the total time lapse by the total number of database records, and it that gave me the average time it took to process each record.


Leave a Reply

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

To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image