Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Last active February 4, 2016 03:48
Show Gist options
  • Save kurozumi/02c939d9820baeac2cc5 to your computer and use it in GitHub Desktop.
Save kurozumi/02c939d9820baeac2cc5 to your computer and use it in GitHub Desktop.
【PHP】シンプルなベンチマーククラス
<?php
namespace Acme;
class Benchmark
{
/**
* markers
*
* @var array
*/
private $marker = [];
/**
* set marking
*
* @param string|integer $name
*/
public function mark($name)
{
$this->marker[$name] = microtime(true);
}
/**
* seek marking
*
* @param string|integer $name
* @return float
* @throws \OutOfBoundsException
*/
public function seek($name)
{
if (!isset($this->marker[$name]))
{
throw new \OutOfBoundsException("invalid marker [$name]");
}
return $this->marker[$name];
}
/**
* get elapsed time
*
* @param string|integer $start
* @param string|integer $end
* @param integer $decimals
* @return float
* @throws \OutOfBoundsException
*/
public function getTime($start = 'start', $end = 'end', $decimals = 4)
{
$start_time = $this->seek($start);
$end_time = $this->seek($end);
if ($end_time - $start_time < 0)
{
throw new \OutOfBoundsException("invalid marker [$start]");
}
return number_format($end_time - $start_time, $decimals);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment