Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@swynter-ladbrokes
Created August 7, 2014 00:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swynter-ladbrokes/65495a96fccf93a5ac2a to your computer and use it in GitHub Desktop.
Save swynter-ladbrokes/65495a96fccf93a5ac2a to your computer and use it in GitHub Desktop.
<?php
class Benchmark {
private $task_duration = 5;
private $tasks = [];
private $results = [];
private $duation = 0;
public function __construct() {
}
public function addTask(BenchmarkTask $task) {
$this->tasks[] = $task;
}
public function addTasks(array $tasks) {
foreach ($tasks as $task) {
$this->addTask($task);
}
}
public function getTasks() {
return $this->tasks;
}
public function getResults() {
return $this->results;
}
public function getDuration() {
return $this->duration;
}
public function run($task_duration = null) {
if (is_null($task_duration))
$task_duration = $this->task_duration;
$run_start_time = microtime(true);
foreach ($this->tasks as $task) {
$closure = $task->getClosure();
$task_start_microtime = microtime(true);
$task_start_time = time();
$iterations = 0;
while ((time() - $task_start_time) < $task_duration) {
$closure();
$iterations ++;
}
$duration = microtime(true) - $task_start_microtime;
$this->results[] = new BenchmarkResult($task, $duration, $iterations);
}
$this->duration = microtime(true) - $run_start_time;
}
public function __toString() {
$str = '';
foreach ($this->results as $result)
$str .= $result . PHP_EOL . PHP_EOL;
$str .= 'Ran ' . count($this->tasks) . ' benchmarks in ' . $this->duration . ' seconds';
return $str . PHP_EOL;
}
}
class BenchmarkTask {
private $closure;
private $reflection;
private $source;
private $name;
private $additionalDuration;
public function __construct($name, Closure $closure, $additionalDuration = 0) {
$this->closure = $closure;
$this->name = $name;
$this->additionalDuration = $additionalDuration;
$this->reflection = new ReflectionFunction($closure);
}
public function __toString() {
$str = 'function (';
$params = array();
foreach ($this->reflection->getParameters() as $p) {
$s = '';
if ($p->isArray()) {
$s .= 'array ';
}
else if ($p->getClass()) {
$s .= $p->getClass()->name . ' ';
}
if ($p->isPassedByReference())
$s .= '&';
$s .= '$' . $p->name;
if ($p->isOptional())
$s .= ' = ' . var_export($p->getDefaultValue(), true);
$params[]= $s;
}
$str .= implode(', ', $params);
$str .= ') {' . PHP_EOL;
$lines = file($this->reflection->getFileName());
$tabs = strspn($lines[$this->reflection->getStartLine()], "\t");
for($l = $this->reflection->getStartLine(); $l < $this->reflection->getEndLine(); $l++) {
$str .= substr($lines[$l], $tabs - 1);
}
return substr($str, 0, 1 + strrpos($str, '}'));
}
public function __invoke() {
return $this->reflection->invokeArgs(func_get_args());
}
public function getName() {
return $this->name;
}
public function getClosure() {
return $this->closure;
}
public function getAdditionalDuration() {
return $this->additionalDuration;
}
}
class BenchmarkResult {
private $task;
private $duration;
private $iterations;
public function __construct(BenchmarkTask $task, $duration, $iterations) {
$this->task = $task;
$this->duration = $duration;
$this->iterations = $iterations;
}
public function getDuration() {
return $this->duration;
}
public function getIterations() {
return $this->iterations;
}
public function getTask() {
return $this->task;
}
public function __toString() {
$str = 'Benchmark result for ' . $this->task->getName() . PHP_EOL;
$str .= $this->task . PHP_EOL;
$str .= 'Executed ' . $this->getIterations() . ' iterations in ' . $this->getDuration() . ' (+ ' . $this->task->getAdditionalDuration() . ') seconds';
return $str;
}
}
<?php
require('lib/Benchmark.php');
$data = [
'a,b,c,d,e,f,g,h',
'a ,b, c, d ,e, f, g, h'
];
$start = microtime(true);
$data2 = array_map(function($a) {return preg_replace('/(\s+,|,\s+|\s+,\s+)/', ',', $a);}, $data);
$preg_replace = microtime(time) - $start;
$benchmark = new Benchmark();
$benchmark->addTasks([
new BenchmarkTask('preg_split', function() use ($data) {
foreach ($data as $dat) {
$a = preg_split('/\s*,\s*/', $dat);
}
}),
new BenchmarkTask('explode', function() use ($data) {
foreach ($data as $dat) {
$a = explode(',', $dat);
}
}),
new BenchmarkTask('explode, trim', function() use ($data) {
foreach ($data as $dat) {
$a = array_map('trim', explode(',', $dat));
}
}),
new BenchmarkTask('preg_replace, explode', function() use ($data2) {
foreach ($data2 as $dat) {
$a = explode(',', $dat);
}
}, $preg_replace),
]);
$benchmark->run();
print "$benchmark";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment