Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
【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