Created
August 4, 2011 17:28
-
-
Save pifantastic/1125696 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* Provides methods for time and memory profiling | |
* | |
* $Id$ | |
* | |
* @package Pifanstatic | |
* @author Aaron Forsander | |
* @copyright (c) 2009 Aaron Forsander | |
* @license MIT | |
*/ | |
class profiler { | |
// Internal array of profiles | |
private static $profiles = array(); | |
/** | |
* Start a profile | |
* | |
* @param string the id of the profile | |
* @return void | |
*/ | |
public static function start($id = 'default') | |
{ | |
// initialize profile | |
self::$profiles[$id] = (object) array( | |
'time' => (object) array( | |
'start' => 0, | |
'stop' => 0, | |
'delta' => 0 | |
), | |
'memory' => (object) array( | |
'start' => 0, | |
'stop' => 0, | |
'delta' => 0 | |
) | |
); | |
// start profiling | |
self::$profiles[$id]->memory->start = memory_get_usage(); | |
self::$profiles[$id]->time->start = microtime(TRUE); | |
} | |
/** | |
* Stop a profile | |
* | |
* @param string the id of the profile | |
* @return stdClass profile object | |
*/ | |
public static function stop($id = 'default') | |
{ | |
// stop profiling | |
self::$profiles[$id]->time->stop = microtime(TRUE); | |
self::$profiles[$id]->memory->stop = memory_get_usage(); | |
// calculate time delta | |
self::$profiles[$id]->time->delta = | |
self::$profiles[$id]->time->stop - self::$profiles[$id]->time->start; | |
// calculate memory delta | |
self::$profiles[$id]->memory->delta = | |
self::$profiles[$id]->memory->stop - self::$profiles[$id]->memory->start; | |
// return profile (stdClass) | |
return self::$profiles[$id]; | |
} | |
/** | |
* Retrieve a profile | |
* | |
* @param string the id of the profile | |
* @return stdClass profile object | |
*/ | |
public static function get($id = 'default') | |
{ | |
// return profile if exists | |
return (isset(self::$profiles[$id])) ? self::$profiles[$id] : NULL; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment