Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace app\system;
- use app\system\MicroTime;
- class ElapsedTiming {
- public $break;
- private $startTime = null;
- private $stopTime = null;
- // Constructor for Timing class
- public function __construct($break = "<br />") {
- $this->break = $break;
- // Set timezone
- date_default_timezone_set('UTC');
- }
- // Set start time
- public function start() {
- $this->startTime = new MicroTime();
- }
- // Set stop/end time
- public function stop() {
- $this->stopTime = new MicroTime();
- }
- // Returns time elapsed from start
- public function getElapsedTime($when = null) {
- $nowMT = new MicroTime($when);
- $diff = $nowMT->diff($this->startTime);
- return number_format($diff->elapsedTime(), 3);
- }
- // Returns total execution time
- public function getTotalExecutionTime() {
- if (!$this->stopTime) {
- return false;
- }
- $diff = $this->stopTime->diff($this->startTime);
- return $diff->elapsedTime();
- }
- // Returns start time, stop time and total execution time
- public function getFullStats() {
- if (!$this->stopTime) {
- return false;
- }
- $stats = array();
- $stats['startTime'] = $this->startTime->asDateTimeStr();
- $stats['stopTime'] = $this->stopTime->asDateTimeStr();
- $stats['total_execution_time'] = $this->getTotalExecutionTime();
- return $stats;
- }
- // Prints time elapsed from start
- public function printElapsedTime($prompt = '') {
- echo $this->break . $this->break;
- echo "Elapsed time: " . $this->getElapsedTime();
- echo $this->break . $this->break;
- }
- // Prints total execution time
- public function printTotalExecutionTime() {
- if (!$this->stopTime) {
- return false;
- }
- echo $this->break . $this->break;
- echo "Total execution time: " . $this->getTotalExecutionTime();
- echo $this->break . $this->break;
- }
- // Prints start time, stop time and total execution time
- public function printFullStats($prompt = '') {
- if (!$this->stopTime) {
- return false;
- }
- echo $this->break . $this->break;
- if (!empty($prompt)) {
- echo $prompt;
- echo $this->break;
- }
- echo "Script start date and time : " . $this->startTime->asDateTimeStr();
- echo $this->break;
- echo "Script stop end date and time: " . $this->stopTime->asDateTimeStr();
- echo $this->break;
- echo "Total execution time : " . $this->getTotalExecutionTime();
- echo $this->break . $this->break;
- }
- /*
- * return MicroTime objects
- */
- public function startTime()
- {
- return $this->startTime;
- }
- public function stopTime()
- {
- return $this->stopTime;
- }
- /*
- public function elapsedTime()
- {
- $nowMT = new MicroTime();
- return $nowMT->diff($this->startTime)->elapsedTime();
- }
- *
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement