Created
November 17, 2015 13:33
-
-
Save bordeo/76266149f04f1d5a9934 to your computer and use it in GitHub Desktop.
Estimate Time to Completion
This file contains 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 | |
/** | |
* This class provides the methods to calculate the estimated time to completion | |
* (or ETC) for a iterated procedure, by passing the numer of iterations you will do. | |
* | |
* ----------------------------------------------------------------------------- | |
* Example of usage: | |
* $timer = new ETC(number of iterations you have); | |
* | |
* ITERATION { | |
* $timer->startIterationTimer(); OR $timer->start(); | |
* | |
* YOUR CODE GOES HERE | |
* | |
* $timeForThisIteration = $timer->stopIterationTimer(); OR $timer->stop(); | |
* print $timeForThisIteration; //in seconds | |
* print $timer->getETC(); //in H:i:s | |
* } | |
* | |
* print $timer->getElapsedTime(); OR $timer->elapsed(); | |
* ----------------------------------------------------------------------------- | |
* | |
* @author Davide Barlassina <[email protected]> | |
*/ | |
class ETC { | |
protected $time_start; | |
protected $avgTimeForIteration; | |
protected $remainingIterations; | |
protected $totalIterations; | |
protected $elapsed = 0; | |
protected $iterationIndex = 0; | |
public function __construct($totalIterations) { | |
$this->totalIterations = $totalIterations; | |
$this->remainingIterations = $this->totalIterations; | |
} | |
/** | |
* Starts the timer for a single iteration | |
*/ | |
public function startIterationTimer() { | |
$this->time_start = microtime(true); | |
} | |
/** | |
* Stops the timer and returns the time of execution for a single iteration | |
* @return string | |
*/ | |
public function stopIterationTimer() { | |
$time = round(microtime(true) - $this->time_start, 2); | |
$this->elapsed = $this->elapsed + $time; | |
$this->avgTimeForIteration = $this->elapsed / ($this->iterationIndex+1); | |
$this->remainingIterations = $this->totalIterations - ($this->iterationIndex+1); | |
$this->iterationIndex++; | |
return $time; | |
} | |
/** | |
* Returns the estimated time of completion | |
* @return string | |
*/ | |
public function getETC() { | |
return gmdate("H:i:s", $this->remainingIterations * $this->avgTimeForIteration); | |
} | |
/** | |
* Returns the total elapsed time since the first startIterationTimer() | |
* @return string | |
*/ | |
public function getElapsedTime() { | |
return gmdate("H:i:s", $this->elapsed); | |
} | |
/** | |
* SHORTCUTS | |
*/ | |
public function start() { return $this->startIterationTimer(); } | |
public function stop() { return $this->stopIterationTimer(); } | |
public function elapsed() { return $this->getElapsedTime(); } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment