Skip to content

Instantly share code, notes, and snippets.

@enumag
Created February 11, 2012 20:00
Show Gist options
  • Save enumag/1803899 to your computer and use it in GitHub Desktop.
Save enumag/1803899 to your computer and use it in GitHub Desktop.
PHP Timer
<?php
class Timer {
public static $time = array();
public static $last;
public static function start($name = NULL) {
static $i = 0;
if ($name === NULL) {
$name = $i++;
}
self::$last = $name;
self::$time[$name] = microtime(TRUE);
}
public static function stop($name = NULL) {
if ($name === NULL) {
$name = self::$last;
}
self::$time[$name] = (microtime(TRUE) - self::$time[$name]) * 1000;
}
public static function table() {
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
return;
Timer::stop('page');
echo '<div style="position: fixed; bottom: 30%; right: 15%; z-index: 999; background-color: lightgray; padding: 15px; font-size: 13px;">';
echo '<table>';
foreach (self::$time as $key => $value) {
echo '<tr><td style="padding: 2px 5px">' . $key . '</td><td>' . round($value) . ' ms</td></tr>';
}
echo '</table>';
echo '</div>';
}
}
Timer::start('page');
register_shutdown_function(array('Timer', 'table'));
function start($name) {
Timer::start($name);
}
function stop($name = NULL) {
Timer::stop($name);
}
function flip($name = NULL) {
Timer::stop();
Timer::start($name);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment