Skip to content

Instantly share code, notes, and snippets.

@mrspartak
Created May 13, 2014 07:03
Show Gist options
  • Select an option

  • Save mrspartak/f56ddad129af995c6d3b to your computer and use it in GitHub Desktop.

Select an option

Save mrspartak/f56ddad129af995c6d3b to your computer and use it in GitHub Desktop.
<?
class Bench
{
const defaultName = 'default';
private $_container = array();
public function start($name = self::defaultName)
{
$this->_container[$name] = array(
'time' => gettimeofday(true),
'memory' => memory_get_usage()
);
}
public function finish($name = self::defaultName)
{
list($startTime, $startMemory) = array_values( $this->_container[$name] );
$this->_container[$name] = array(
'time' => gettimeofday(true) - $startTime,
'memory' => memory_get_usage() - $startMemory
);
}
public function result($name = self::defaultName)
{
echo <<<EOT
---- $name <br>
Time: {$this->getTime($name)} <br>
Memory: {$this->getMemory($name)} <br>
---- <br>
EOT;
}
public function getTime($name = self::defaultName)
{
return $this->_container[$name]['time'];
}
public function getMemory($name = self::defaultName)
{
return $this->_container[$name]['memory'];
}
}
@mrspartak
Copy link
Author

Usage:

<?

$b = new Bench;

$b->start('row');
//
// some action
//
$b->finish('row');

// get Time
echo $b->getTime('row');
//get Memory
echo $b->getMemory('row');

// get Output
$b->result('row');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment