Skip to content

Instantly share code, notes, and snippets.

@bubach
Created October 11, 2014 21:46
Show Gist options
  • Save bubach/bd675452ce7c5789553b to your computer and use it in GitHub Desktop.
Save bubach/bd675452ce7c5789553b to your computer and use it in GitHub Desktop.
ram & time
<?php
self::timer('wmvc_app_start'); // set named start points
self::ram('wmvc_app_start');
//...
/**
* timer
*
* get/set timer values
*
* @access public
* @param string $id the timer id to set (or compare with $id2)
* @param string $id2 the timer id to compare with $id
* @return float difference of two times
*/
public static function timer ($id = null, $id2 = null) {
static $times = array();
if ($id !== null && $id2 !== null) {
return (isset($times[$id]) && isset($times[$id2])) ? ($times[$id2] - $times[$id]) : false;
} elseif ($id !== null) {
return $times[$id] = microtime(true);
}
return false;
}
/**
* ram
*
* get/set ram usage value
*
* @access public
* @param string $id the ram id to set (or compare with $id2)
* @param string $id2 the ram id to compare with $id
* @return string formatted difference of usage
*/
public static function ram ($id = null, $id2 = null) {
static $ram = array();
static $unit = array('b', 'kb', 'mb', 'gb', 'tb', 'pb');
if ($id !== null && $id2 !== null) {
$returnValue = (isset($ram[$id]) && isset($ram[$id2])) ? ($ram[$id2] - $ram[$id]) : false;
if ($returnValue !== false) {
return @round($returnValue/pow(1024, ($i = floor(log($returnValue, 1024)))), 2).' '.$unit[$i];
} else {
return false;
}
} elseif ($id !== null) {
$ram[$id] = memory_get_usage();
return @round($ram[$id]/pow(1024, ($i = floor(log($ram[$id], 1024)))), 2).' '.$unit[$i];
}
return false;
}
//...
self::timer('wmvc_app_end'); // set end points
self::ram('wmvc_app_end');
echo self::ram('wmvc_app_start', 'wmvc_app_end'); // echo the difference
echo self::timer('wmvc_app_start', 'wmvc_app_end');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment