note 50962 added to function.microtime

From: Date: Tue, 15 Mar 2005 02:31:43 +0000
Subject: note 50962 added to function.microtime
Groups: php.notes 
Request: Send a blank email to php-notes+get-86447@lists.php.net to get a copy of this message
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" ?> ---- Manual Page -- http://www.php.net/manual/en/function.microtime.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+50962 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50962&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50962&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50962&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50962&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50962&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+50962&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#86447) next »