Skip to content

Instantly share code, notes, and snippets.

@phybros
Last active August 31, 2017 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save phybros/5766062 to your computer and use it in GitHub Desktop.
Save phybros/5766062 to your computer and use it in GitHub Desktop.
A simple "StopWatch" class to measure PHP execution time. The class can handle multiple separate timers at the same time.
<?php
class StopWatch {
/**
* @var $start float The start time of the StopWatch
*/
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];
}
}
@phybros
Copy link
Author

phybros commented Sep 26, 2013

Usage:

<?php
  StopWatch::start();
  sleep(5); // perform long running operation
  echo sprintf("Operation completed in %s seconds", StopWatch::elapsed());

@edwardbrosens
Copy link

You might want to correctly place opening brackets for the function bodies.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment