Last active
February 5, 2016 04:49
-
-
Save kurozumi/879f1040a6cc5f64092c to your computer and use it in GitHub Desktop.
【PHP】SeekableIteratorインターフェイスを使ったシンプルなベンチマーククラス
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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