Last active
August 29, 2015 14:24
-
-
Save sagebind/8d848c4461ce21d480a3 to your computer and use it in GitHub Desktop.
A simple timer class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* The MIT License (MIT) | |
* | |
* Copyright (c) 2015 Stephen Coakley | |
* | |
* Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions: | |
* | |
* The above copyright notice and this permission notice shall be included in | |
* all copies or substantial portions of the Software. | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
* THE SOFTWARE. | |
*/ | |
/** | |
* A class for measuring precise timings. | |
*/ | |
class Timer | |
{ | |
/** | |
* @var int The timestamp the timer started at. | |
*/ | |
protected $startTime = 0; | |
/** | |
* @var int[] List of lap times. | |
*/ | |
protected $laps = array(); | |
/** | |
* @var bool Whether the timer is currently running. | |
*/ | |
protected $running = false; | |
/** | |
* Checks if the timer is running. | |
* | |
* @return bool True if the timer is running, otherwise false. | |
*/ | |
public function isRunning() | |
{ | |
return $this->running; | |
} | |
/** | |
* Starts the timer. | |
*/ | |
public function start() | |
{ | |
$this->running = true; | |
if ($this->startTime === 0) { | |
$this->startTime = microtime(true) * 1000; | |
} | |
} | |
/** | |
* Stops the timer if it is currently running. | |
* | |
* @return int The time the timer stopped at. | |
*/ | |
public function stop() | |
{ | |
$this->running = false; | |
return $this->getTimeElapsed(); | |
} | |
/** | |
* Adds a lap marker. | |
* | |
* @return int The time of the lap. | |
*/ | |
public function lap() | |
{ | |
if ($this->running) { | |
$time = $this->getTimeElapsed(); | |
array_push($this->laps, $time); | |
return $time; | |
} | |
} | |
/** | |
* Resets the timer back to zero. | |
*/ | |
public function reset() | |
{ | |
$this->startTime = 0; | |
$this->laps = array(); | |
} | |
/** | |
* Gets the amount of time elapsed in milliseconds. | |
* | |
* @return int The number of milliseconds past since the timer began. | |
*/ | |
public function getTimeElapsed() | |
{ | |
return (microtime(true) * 1000) - $this->startTime; | |
} | |
/** | |
* Gets the lap times in milliseconds. | |
* | |
* @return int[] A list of lap times since the timer started. | |
*/ | |
public function getLaps() | |
{ | |
return $this->laps; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment