Simple "StopWatch" class to measure PHP execution time. The class can handle multiple separate timers at the same time
<?php | |
class StopWatch { | |
/** | |
* @var $startTimes array The start times of the StopWatches | |
*/ | |
private static $startTimes = array(); | |
/** | |
* Start the timer | |
* | |
* @param $timerName string The name of the timer | |
* @return void | |
*/ | |
public static function start($timerName = 'default') { | |
self::$startTimes[$timerName] = microtime(true); | |
} | |
/** | |
* Get the elapsed time in seconds | |
* | |
* @param $timerName string The name of the timer to start | |
* @return float The elapsed time since start() was called | |
*/ | |
public static function elapsed($timerName = 'default') { | |
return microtime(true) - self::$startTimes[$timerName]; | |
} | |
} | |
// start the timer | |
StopWatch::start(); | |
// your script - this is an example | |
sleep(2); | |
// check how long 2 seconds is... | |
echo 'Elapsed time: ' . StopWatch::elapsed() . ' seconds'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
how can i start using a button and stop using also a button please help!