} } // Key not in array return false; } } class BenchmarkSetLookup extends AbstractBenchmark { public function execute() { $randKey = rand(1000, 10000); return isset($this->testSet[$randKey]); } } // Create a new benchmark instance $phpench = new mre\PHPench(new \mre\PHPench\Aggregator\MedianAggregator()); // Use GnuPlot for output $oOutput = new \mre\PHPench\Output\GnuPlotOutput('test2.png', 1024, 768); // Alternatively, print the values to the terminal //$oOutput = new \mre\PHPench\Output\CliOutput(); $oOutput->setTitle('Compare set and list lookup in PHP'); $phpench->setOutput($oOutput); // Add your test to the instance $phpench->addBenchmark(new BenchmarkSetLookup(), 'set'); $phpench->addBenchmark(new BenchmarkListLookup(), 'list'); // Run the benchmark and plot the results in realtime. // With the second parameter you can specify // the start, end and step for each call $phpench->setInput(range(1, pow(2, 16), 1024)); $phpench->setRepetitions(4); $phpench->run();
class BenchmarkIsset extends AbstractBenchmark { public function execute() { $bla = isset($this->test['doesnotexist']); } } class BenchmarkArrayKeyExists extends AbstractBenchmark { public function execute() { $bla = array_key_exists('doesnotexist', $this->test); } } // Create a new benchmark instance $phpench = new mre\PHPench(new \mre\PHPench\Aggregator\MedianAggregator()); // Use GnuPlot for output $oOutput = new \mre\PHPench\Output\GnuPlotOutput('test2.png', 1024, 768); // Alternatively, print the values to the terminal //$oOutput = new \mre\PHPench\Output\CliOutput(); $oOutput->setTitle('Compare isset and array_key_exists'); $phpench->setOutput($oOutput); // Add your test to the instance $phpench->addBenchmark(new BenchmarkIsset(), 'isset'); $phpench->addBenchmark(new BenchmarkArrayKeyExists(), 'array_key_exists'); // Run the benchmark and plot the results in realtime. // With the second parameter you can specify // the start, end and step for each call $phpench->setInput(range(1, pow(2, 16), 1024)); $phpench->setRepetitions(4); $phpench->run();
class BenchmarkArrayFlip extends AbstractBenchmark { public function execute() { $test = array_flip(array_flip($this->test)); } } class BenchmarkArrayUnique extends AbstractBenchmark { public function execute() { $test = array_unique($this->test); } } // Create a new benchmark instance $phpench = new mre\PHPench(new \mre\PHPench\Aggregator\MedianAggregator()); // Use GnuPlot for output $oOutput = new \mre\PHPench\Output\GnuPlotOutput('test2.png', 1024, 768); // Alternatively, print the values to the terminal //$oOutput = new \mre\PHPench\Output\CliOutput(); $oOutput->setTitle('Compare array_flip and array_unique'); $phpench->setOutput($oOutput); // Add your test to the instance $phpench->addBenchmark(new BenchmarkArrayFlip(), 'array_flip'); $phpench->addBenchmark(new BenchmarkArrayUnique(), 'array_unique'); // Run the benchmark and plot the results in realtime. // With the second parameter you can specify // the start, end and step for each call $phpench->setInput(range(1, pow(2, 16), 1024)); $phpench->setRepetitions(4); $phpench->run();
} $pivot = $seq[0]; $low = $high = array(); $length = count($seq); for ($i = 1; $i < $length; $i++) { if ($seq[$i] <= $pivot) { $low[] = $seq[$i]; } else { $high[] = $seq[$i]; } } return array_merge($this->quicksort($low), array($pivot), $this->quicksort($high)); } public function execute() { $sorted = $this->quicksort($this->test); } } // Create a new benchmark instance $phpench = new mre\PHPench(new \mre\PHPench\Aggregator\MedianAggregator()); $output = new \mre\PHPench\Output\GnuPlotOutput('sorting_algorithms.png', 1024, 768); $output->setTitle('Sorting Algorithms'); $phpench->setOutput($output); // Add your test to the instance $phpench->addBenchmark(new BenchmarkBubbleSort(), 'bubblesort'); $phpench->addBenchmark(new BenchmarkQuickSort(), 'quicksort'); // Run the benchmark and plot the results in realtime. // With the second parameter you can specify // the start, end and step for each call $phpench->setInput(range(1, pow(2, 16), 128)); $phpench->run();
{ } } class BenchmarkSingleQuotes extends AbstractBenchmark { public function execute() { $test = 'hello' . 'this' . 'is' . 'a' . 'test'; } } class BenchmarkDoubleQuotes extends AbstractBenchmark { public function execute() { $test = "hello" . "this" . "is" . "a" . "test"; } } // Create a new benchmark instance $phpench = new mre\PHPench(new \mre\PHPench\Aggregator\MedianAggregator()); $output = new \mre\PHPench\Output\GnuPlotOutput('test4.png', 1024, 768); $output->setTitle('Compare single quote and double quote strings'); $phpench->setOutput($output); // Add your test to the instance $phpench->addBenchmark(new BenchmarkSingleQuotes(), 'single_quotes'); $phpench->addBenchmark(new BenchmarkDoubleQuotes(), 'double_quotes'); // Run the benchmark and plot the results in realtime. // With the second parameter you can specify // the start, end and step for each call $phpench->setInput(range(1, pow(2, 16), 1024)); $phpench->setRepetitions(4); $phpench->run(True);
class BenchmarkStringReplaceArrayValue extends AbstractBenchmark { public function execute() { $this->text = str_replace(array_keys($this->placeholders), array_values($this->placeholders), $this->text); } } class BenchmarkPregReplaceCallback extends AbstractBenchmark { public function execute() { $this->text = preg_replace_callback('/(\\$[\\w\\d]+)\\s/', function ($matches) { return isset($this->placeholders[$matches[0]]) ? $this->placeholders[$matches[0]] : $matches[0]; }, $this->text); } } // Create a new benchmark instance $phpench = new mre\PHPench(new \mre\PHPench\Aggregator\MedianAggregator()); $output = new \mre\PHPench\Output\GnuPlotOutput('test3.png', 1024, 768); $output->setTitle('Compare placeholder replacement'); $phpench->setOutput($output); // Add your test to the instance $phpench->addBenchmark(new BenchmarkStringReplaceForeach(), 'TestStringReplaceForeach'); $phpench->addBenchmark(new BenchmarkStringReplaceArrayValue(), 'TestStringReplaceArrayValue'); $phpench->addBenchmark(new BenchmarkPregReplaceCallback(), 'TestPregReplaceCallback'); // Run the benchmark and plot the results in realtime. // With the second parameter you can specify // the start, end and step for each call $phpench->setInput(range(0, 200, 2)); $phpench->setRepetitions(10); $phpench->run();