Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Last active February 5, 2016 04:49
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kurozumi/879f1040a6cc5f64092c to your computer and use it in GitHub Desktop.
【PHP】SeekableIteratorインターフェイスを使ったシンプルなベンチマーククラス
<?php
namespace Acme;
class Benchmark implements \SeekableIterator
{
private $decimal = 4;
private $marker = [];
private $timestamp = [];
public function mark($name)
{
$this->marker[$name] = microtime(true);
}
public function setDecimal($decimal)
{
$this->decimal = $decimal;
}
protected function setTimestamp($marks)
{
if(count($marks) !== 2)
{
throw new \InvalidArgumentException('invalid argument');
}
$this->timestamp = [];
foreach($marks as $name)
{
$this->timestamp[] = $this->seek($name);
}
sort($this->timestamp);
}
public function getTimestamp()
{
return $this->timestamp;
}
public function getTime(array $marks)
{
$this->setTimestamp($marks);
return number_format($this->timestamp[1] - $this->timestamp[0], $this->decimal);
}
public function seek($name)
{
if (!isset($this->marker[$name]))
{
throw new \OutOfBoundsException("invalid marker [$name]");
}
return $this->marker[$name];
}
public function current()
{
return current($this->marker);
}
public function key()
{
return key($this->marker);
}
public function next()
{
next($this->marker);
}
public function rewind()
{
return reset($this->marker);
}
public function valid()
{
return key($this->marker) !== null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment