note 50277 deleted from function.microtime by tularis

From: Date: Sun, 22 Jun 2008 12:16:10 +0000
Subject: note 50277 deleted from function.microtime by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-140709@lists.php.net to get a copy of this message
Note Submitter: David Genord II ---- Here is a little more versitile version of the chronometer script below. /************************************************************ * * void | float chronometer($timer) * * Works exactly like a stop watch, ie. starts if stopped * and stops if started * * Call the function a first time to start the chronometer. * The next call to the function will return the number of * milliseconds elapsed since the chronometer was started * (rounded to three decimal places). The next call * will start the chronometer again from where it finished. * * Multiple timers can be used by creating multiple $timer * variables. * * An example script would be: * * chronometer($timer1); * DO STUFF HERE * chronometer($timer2); * chronometer($timer3); * DO MORE STUFF * echo chronometer($timer1); * DO SOMETHING * echo chronometer($timer3); * DO SOMETHING * echo chronometer($timer2); * * The timer variables do not need to be declared or * initialized before use * ************************************************************/ function chronometer(&$CHRONO_STARTTIME) { $now = microtime(TRUE); // float, in _seconds_ if(isset($CHRONO_STARTTIME['running'])) { if($CHRONO_STARTTIME['running']) { /* Stop the chronometer : return the amount of time since it was started, in ms with a precision of 3 decimal places. 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 */ $CHRONO_STARTTIME['elapsed'] += round($now - $CHRONO_STARTTIME['temp'], 3); $CHRONO_STARTTIME['running'] = false; return $CHRONO_STARTTIME['elapsed']; } else { $CHRONO_STARTTIME['running'] = true; $CHRONO_STARTTIME['temp'] = $now; } } else { // Start the chronometer : save the starting time $CHRONO_STARTTIME = array(); $CHRONO_STARTTIME['running'] = true; $CHRONO_STARTTIME['elapsed'] = 0; $CHRONO_STARTTIME['temp'] = $now; } }

« previous php.notes (#140709) next »