Created
July 28, 2012 07:06
-
-
Save cam-gists/3192210 to your computer and use it in GitHub Desktop.
PHP: Benchmarking / Timing Class
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 | |
/** | |
* Timing Class for benchmarking in PHP | |
* @ref: http://www.if-not-true-then-false.com/2010/php-timing-class-class-for-measure-php-scripts-execution-time-and-php-web-page-load-time/ | |
*/ | |
class Timing { | |
private $break; | |
private $start_time; | |
private $stop_time; | |
// 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->start_time = microtime(true); | |
} | |
// Set stop/end time | |
public function stop() { | |
$this->stop_time = microtime(true); | |
} | |
// Returns time elapsed from start | |
public function getElapsedTime() { | |
return $this->getExecutionTime(microtime(true)); | |
} | |
// Returns total execution time | |
public function getTotalExecutionTime() { | |
if (!$this->stop_time) { | |
return false; | |
} | |
return $this->getExecutionTime($this->stop_time); | |
} | |
// Returns start time, stop time and total execution time | |
public function getFullStats() { | |
if (!$this->stop_time) { | |
return false; | |
} | |
$stats = array(); | |
$stats['start_time'] = $this->getDateTime($this->start_time); | |
$stats['stop_time'] = $this->getDateTime($this->stop_time); | |
$stats['total_execution_time'] = $this->getExecutionTime($this->stop_time); | |
return $stats; | |
} | |
// Prints time elapsed from start | |
public function printElapsedTime() { | |
echo $this->break . $this->break; | |
echo "Elapsed time: " . $this->getExecutionTime(microtime(true)); | |
echo $this->break . $this->break; | |
} | |
// Prints total execution time | |
public function printTotalExecutionTime() { | |
if (!$this->stop_time) { | |
return false; | |
} | |
echo $this->break . $this->break; | |
echo "Total execution time: " . $this->getExecutionTime($this->stop_time); | |
echo $this->break . $this->break; | |
} | |
// Prints start time, stop time and total execution time | |
public function printFullStats() { | |
if (!$this->stop_time) { | |
return false; | |
} | |
echo $this->break . $this->break; | |
echo "Script start date and time: " . $this->getDateTime($this->start_time); | |
echo $this->break; | |
echo "Script stop end date and time: " . $this->getDateTime($this->stop_time); | |
echo $this->break . $this->break; | |
echo "Total execution time: " . $this->getExecutionTime($this->stop_time); | |
echo $this->break . $this->break; | |
} | |
// Format time to date and time | |
private function getDateTime($time) { | |
return date("Y-m-d H:i:s", $time); | |
} | |
// Get execution time by timestamp | |
private function getExecutionTime($time) { | |
return $time - $this->start_time; | |
} | |
} | |
?> | |
// USAGE #1 | |
<?php | |
// Create new Timing class with \n break | |
$timing = new Timing("\n"); | |
// Start timing | |
$timing->start(); | |
// Loop ten rounds and sleep one second per round | |
for ($i=1;$i<=10;$i++) { | |
echo $i . "\t"; sleep(1); | |
// Print elapsed time every 2 rounds | |
if ($i%2==0) { | |
$timing->printElapsedTime(); | |
} | |
} | |
// Stop/end timing | |
$timing->stop(); | |
// Print only total execution time | |
$timing->printTotalExecutionTime(); | |
// Print full stats | |
$timing->printFullStats(); | |
?> | |
// Usage #2 | |
<?php | |
// Create new Timing class with \n break | |
$timing = new Timing("\n"); | |
// Start timing | |
$timing->start(); | |
$elapsed_times = array(); | |
// Loop ten rounds and sleep one second per round | |
for ($i=1;$i<=10;$i++) { | |
echo $i . "\t"; sleep(1); | |
// Get elapsed time every rounds | |
$elapsed_times[$i] = $timing->getElapsedTime(); | |
} | |
echo "\n\n"; | |
// Stop/end timing | |
$timing->stop(); | |
// Get full stats | |
$times = $timing->getFullStats(); | |
foreach ($elapsed_times as $key => $t) { | |
$t2; | |
if (isset($elapsed_times[$key-1])) { | |
$t2 = $t - $elapsed_times[$key-1]; | |
} | |
else { | |
$t2 = $t; | |
} | |
echo "Round: " . $key . " Time: " . $t2 . "\n"; | |
} | |
echo "Total execution time: " . $times['total_execution_time'] . "\n"; | |
echo $times['start_time'] . " - " . $times['stop_time'] . "\n"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment