Last active
February 4, 2016 03:48
-
-
Save kurozumi/02c939d9820baeac2cc5 to your computer and use it in GitHub Desktop.
【PHP】シンプルなベンチマーククラス
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 | |
{ | |
/** | |
* 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