Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
【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
You can’t perform that action at this time.