This is a simple function that always comes in very handy in debugging and logging PHP code. ExecTime() keeps track of one or more processes, and can round the result up to your desired level of decimal point precision.

ExecTime() starts counting the first time you call it, and from that point on it returns the elapsed time every time you call it again.

Here is the function, along with a couple of usage examples:

//Usage execTime(); //start the counter //perform some lengthy process print "Done in " . execTime(); //seconds //Advanced usage for tracking various processes execTime('entire job'); //start the counter //perform some lengthy process execTime('load database'); //start the counter //perform some lengthy process print "Data loaded in " . execTime('load database'); //seconds //perform some lengthy process print "Entire job done in " . execTime('entire job'); //tracked from the beginning of the job //execTime() used for timing execution function execTime($id = '0', $round = 4, $reset = FALSE){ //this function is called at least twice. //The first time it starts the counter and // the second time it returns the time elapsed in micro secs //ids are used to keep track of multiple processes static $data = array(); //keep track of data if(!$data[$id] or $reset){ $data[$id] = microtime(true); return 0; } else { return number_format((microtime(true)-$data[$id]), $round); } }

Comments

Really helpful...good attempt to track the execution time of scripts...