| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Trait Datafeedr_Timer |
| 5 |
* |
| 6 |
* Imports timer functionality into any class. |
| 7 |
* |
| 8 |
* @since 1.0.72 |
| 9 |
*/ |
| 10 |
trait Datafeedr_Timer { |
| 11 |
|
| 12 |
/** |
| 13 |
* If used and set to TRUE, microtime() will return a float instead |
| 14 |
* of a string, as described in the return values section below. |
| 15 |
* |
| 16 |
* @since 1.0.72 |
| 17 |
* @access public |
| 18 |
* @var bool $microtime_as_float |
| 19 |
*/ |
| 20 |
public $microtime_as_float = true; |
| 21 |
|
| 22 |
/** |
| 23 |
* Start time. |
| 24 |
* |
| 25 |
* @since 1.0.72 |
| 26 |
* @access protected |
| 27 |
* @var float $time_start |
| 28 |
*/ |
| 29 |
protected $time_start = 0; |
| 30 |
|
| 31 |
/** |
| 32 |
* Stop time. |
| 33 |
* |
| 34 |
* @since 1.0.72 |
| 35 |
* @access protected |
| 36 |
* @var float $time_stop |
| 37 |
*/ |
| 38 |
protected $time_stop = 0; |
| 39 |
|
| 40 |
/** |
| 41 |
* Starts the timer. |
| 42 |
* |
| 43 |
* @since 1.0.72 |
| 44 |
*/ |
| 45 |
public function start_timer() { |
| 46 |
$this->time_start = microtime( $this->microtime_as_float ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Stops the timer. |
| 51 |
* |
| 52 |
* @since 1.0.72 |
| 53 |
*/ |
| 54 |
public function stop_timer() { |
| 55 |
$this->time_stop = microtime( $this->microtime_as_float ); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Return rounded elapsed time. |
| 60 |
* |
| 61 |
* @param int $precision Optional. How much to round float value. Default 2. |
| 62 |
* |
| 63 |
* @return float |
| 64 |
*/ |
| 65 |
public function execution_time( $precision = 2 ) { |
| 66 |
$time = $this->elapsed_time(); |
| 67 |
|
| 68 |
return round( $time, $precision ); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Return elapsed time. |
| 73 |
* |
| 74 |
* @return float |
| 75 |
*/ |
| 76 |
public function elapsed_time() { |
| 77 |
return ( $this->time_stop - $this->time_start ); |
| 78 |
} |
| 79 |
} |