Advertisement
rfv123

Elapsed Timing - Class

Dec 9th, 2015
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.12 KB | None | 0 0
  1. <?php
  2. namespace app\system;
  3.  
  4. use app\system\MicroTime;
  5.  
  6. class ElapsedTiming {
  7.  
  8.     public $break;
  9.  
  10.     private $startTime = null;
  11.  
  12.     private $stopTime = null;
  13.  
  14.     // Constructor for Timing class
  15.     public function __construct($break = "<br />") {
  16.         $this->break = $break;
  17.         // Set timezone
  18.         date_default_timezone_set('UTC');
  19.     }
  20.  
  21.     // Set start time
  22.     public function start() {
  23.         $this->startTime = new MicroTime();
  24.     }
  25.  
  26.     // Set stop/end time
  27.     public function stop() {
  28.         $this->stopTime = new MicroTime();
  29.     }
  30.  
  31.     // Returns time elapsed from start
  32.     public function getElapsedTime($when = null) {
  33.         $nowMT = new MicroTime($when);
  34.         $diff = $nowMT->diff($this->startTime);
  35.         return number_format($diff->elapsedTime(), 3);
  36.  
  37.     }
  38.  
  39.     // Returns total execution time
  40.     public function getTotalExecutionTime() {
  41.         if (!$this->stopTime) {
  42.             return false;
  43.         }
  44.         $diff = $this->stopTime->diff($this->startTime);
  45.         return $diff->elapsedTime();
  46.     }
  47.  
  48.     // Returns start time, stop time and total execution time
  49.     public function getFullStats() {
  50.         if (!$this->stopTime) {
  51.             return false;
  52.         }
  53.  
  54.         $stats = array();
  55.         $stats['startTime'] = $this->startTime->asDateTimeStr();
  56.         $stats['stopTime'] = $this->stopTime->asDateTimeStr();
  57.         $stats['total_execution_time'] = $this->getTotalExecutionTime();
  58.  
  59.         return $stats;
  60.     }
  61.  
  62.     // Prints time elapsed from start
  63.     public function printElapsedTime($prompt = '') {
  64.         echo $this->break . $this->break;
  65.         echo "Elapsed time: " . $this->getElapsedTime();
  66.         echo $this->break . $this->break;
  67.     }
  68.  
  69.     // Prints total execution time
  70.     public function printTotalExecutionTime() {
  71.         if (!$this->stopTime) {
  72.             return false;
  73.         }
  74.  
  75.         echo $this->break . $this->break;
  76.         echo "Total execution time: " . $this->getTotalExecutionTime();
  77.         echo $this->break . $this->break;
  78.     }
  79.  
  80.     // Prints start time, stop time and total execution time
  81.     public function printFullStats($prompt = '') {
  82.         if (!$this->stopTime) {
  83.             return false;
  84.         }
  85.  
  86.         echo $this->break . $this->break;
  87.         if (!empty($prompt)) {
  88.               echo $prompt;
  89.               echo $this->break;
  90.         }
  91.         echo "Script start date and time   : " . $this->startTime->asDateTimeStr();
  92.         echo $this->break;
  93.         echo "Script stop end date and time: " . $this->stopTime->asDateTimeStr();
  94.         echo $this->break;
  95.         echo "Total execution time         : " . $this->getTotalExecutionTime();
  96.         echo $this->break . $this->break;
  97.     }
  98.  
  99.     /*
  100.      * return MicroTime objects
  101.      */
  102.     public function startTime()
  103.     {
  104.         return $this->startTime;
  105.     }
  106.  
  107.     public function stopTime()
  108.     {
  109.         return $this->stopTime;
  110.     }
  111.  
  112.     /*
  113.     public function elapsedTime()
  114.     {
  115.         $nowMT = new MicroTime();
  116.         return $nowMT->diff($this->startTime)->elapsedTime();
  117.     }
  118.      *
  119.      */
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement