Skip to content

Instantly share code, notes, and snippets.

@foowie
Created January 22, 2011 14:20
Show Gist options
  • Select an option

  • Save foowie/791146 to your computer and use it in GitHub Desktop.

Select an option

Save foowie/791146 to your computer and use it in GitHub Desktop.
Class for time measuring
<?php
/**
* @author Daniel Robenek
* @license MIT
* @since 2011
*/
namespace Debug;
use Nette\Object;
use Nette\Debug;
/**
* Class for time measuring
*/
class Timer extends Object {
/**
* Static timers
* @var Timer
*/
protected static $timers = array();
/**
* Start time
* @var long
*/
protected $time;
/**
* Time diff between stop and start in us
* @var long
*/
protected $diff = 0;
/**
* Creates new timer
* @param bool $start Start measuring?
*/
function __construct($start = true) {
if($start)
$this->start();
}
/**
* Starts measuring
*/
public function start() {
$this->time = microtime();
}
/**
* Stops measuring
* @return long time in ms
*/
public function stop() {
list($oldus, $olds) = explode(" ", $this->time);
list($newus, $news) = explode(" ", microtime());
$s = $news - $olds;
$us = $newus - $oldus;
$this->diff = $s + $us;
return $this->getTime();
}
/**
* Get time diff
* @return long Measured time in ms
*/
public function getTime() {
return $this->diff * 1000;
}
/**
* Dump measured time in ms
* @param string $name Description of value
*/
public function barDump($name = "Timer") {
Debug::barDump(round($this->getTime(), 5) . " ms", $name);
}
/**
* Creates new timer
* @param string $name Name of timer
* @param bool $start Start measuring?
* @return Timer
*/
public static function create($name = "timer", $start = true) {
self::$timers[$name] = new Timer($start);
return self::$timers[$name];
}
/**
* Get static timer
* @param string Name of timer
* @return Timer
*/
public static function getTimer($name = "timer") {
return self::$timers[$name];
}
/**
* Stop timer and dump time on bar
* @param string $name Name of timer
*/
public static function stopAndDump($name = "timer") {
self::$timers[$name]->stop();
self::$timers[$name]->barDump($name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment