Skip to content

Instantly share code, notes, and snippets.

@tyler-sommer
Last active April 5, 2020 18:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tyler-sommer/5275511 to your computer and use it in GitHub Desktop.
Save tyler-sommer/5275511 to your computer and use it in GitHub Desktop.
Benchmark between SplFixedArray and regular PHP arrays with 10,000 very small elements.
<?php
/**
* A Simple Operation Benchmark Class
*
* @author Tyler Sommer
* @license WTFPL
*/
class Benchmark {
protected $_length;
protected $_tests = array();
protected $_results = array();
/**
* @param int $length The number of iterations per test
*/
public function __construct($length = 1000) {
$this->_length = $length;
}
/**
* Register a test
*
* @param string $name (Friendly) Name of the test
* @param callback $callback A valid callback
*/
public function register($name, $callback) {
$this->_tests[$name] = $callback;
}
/**
* Execute the registered tests and display the results
*/
public function execute() {
$adjustment = round($this->_length * .1, 0);
echo "Running " . count($this->_tests) . " tests, {$this->_length} times each...\nThe {$adjustment} highest and lowest results will be disregarded.\n\n";
foreach ($this->_tests as $name => $test) {
$results = array();
for ($x = 0; $x < $this->_length; $x++) {
ob_start();
$start = time() + microtime();
call_user_func($test);
$results[] = round((time() + microtime()) - $start, 10);
ob_end_clean();
}
sort($results);
// remove the lowest and highest 10% (leaving 80% of results)
for ($x = 0; $x < $adjustment; $x++) {
array_shift($results);
array_pop($results);
}
$avg = array_sum($results) / count($results);
echo "For {$name}, out of " . count($results) . " runs, average time was: " . sprintf("%.10f", $avg) . " secs.\n";
$this->_results[$name] = $avg;
}
asort($this->_results);
reset($this->_results);
$fastestResult = each($this->_results);
reset($this->_results);
echo "\n\nResults:\n";
printf("%-25s\t%-20s\t%-20s\t%s\n", "Test Name", "Time", "+ Interval", "Change");
foreach ($this->_results as $name => $result) {
$interval = $result - $fastestResult["value"];
$change = round((1 - $result / $fastestResult["value"]) * 100, 0);
if ($change == 0) {
$change = 'baseline';
} else {
$faster = true; // Cant really ever be faster, now can it
if ($change < 0) {
$faster = false;
$change *= -1;
}
$change .= '% ' . ($faster ? 'faster' : 'slower');
}
printf("%-25s\t%-20s\t%-20s\t%s\n", $name, sprintf("%.10f", $result), "+" . sprintf("%.10f", $interval), $change);
}
}
}
<?php
require_once __DIR__ . '/Benchmark.php';
$bm = new Benchmark(5000);
$bm->register('Fixed array', function() {
$arr = new \SplFixedArray(10000);
for ($x = 0; $x < 10000; $x++) {
$arr[$x] = $x;
}
});
$bm->register('Regular array', function() {
$arr = array();
for ($x = 0; $x < 10000; $x++) {
$arr[$x] = $x;
}
});
$bm->execute();
/*
Output: (on PHP 5.4.12)
Running 2 tests, 5000 times each...
The 500 highest and lowest results will be disregarded.
For Fixed array, out of 4000 runs, average time was: 0.0026321328 secs.
For Regular array, out of 4000 runs, average time was: 0.0037934296 secs.
Results:
Test Name Time + Interval Change
Fixed array 0.0026321328 +0.0000000000 baseline
Regular array 0.0037934296 +0.0011612968 44% slower
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment