note 50962 deleted from function.microtime by tularis

From: Date: Sun, 22 Jun 2008 12:15:36 +0000
Subject: note 50962 deleted from function.microtime by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-140707@lists.php.net to get a copy of this message
Note Submitter: edwardzyang at thewritingpot dot com ---- Here is a modified version of the chronometer functions. It acts like a "lap watch", immediately starting another timing session right after you call it. It also lets you specify the return value to be in seconds or in milliseconds: this is useful when a microsecond() timing would return a negative value. <?php /* * Slightly modified from http://www.php.net/manual/en/function.microtime.php, adds * support for Second values (as microtime can cause problems when you run extended loops). * It also is "unstopped" timing, that is, the timer never sleeps. */ $CHRONO_STARTTIME = 0; define("RET_TIME", "ms"); //Can be set to "ms" for milliseconds or "s" for seconds function chronometer() { global $CHRONO_STARTTIME; $now = microtime(TRUE); // float, in _seconds_ if (RET_TIME === 's') { $now = $now + time(); $malt = 1; $round = 7; } elseif (RET_TIME === 'ms') { $malt = 1000; $round = 3; } else { die("Unsupported RET_TIME value"); } if ($CHRONO_STARTTIME > 0) { /* Stop the chronometer : return the amount of time since it was started, in ms with a precision of 3 decimal places, and reset the start time. We could factor the multiplication by 1000 (which converts seconds into milliseconds) to save memory, but considering that floats can reach e+308 but only carry 14 decimals, this is certainly more precise */ $retElapsed = round($now * $malt - $CHRONO_STARTTIME * $malt, $round); $CHRONO_STARTTIME = $now; return $retElapsed; } else { // Start the chronometer : save the starting time $CHRONO_STARTTIME = $now; return 0; } } ?> It can be used like this: <?php chronometer(); //Do stuff; echo chronometer(); //this will return the time taken to "Do stuff" //Do more stuff echo chronometer(); //this will return the time taken to "Do more stuff" ?>

« previous php.notes (#140707) next »