Skip to content

Instantly share code, notes, and snippets.

@tyler-sommer
Last active April 5, 2020 18:51

Revisions

  1. tyler-sommer revised this gist Mar 30, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion test.php
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@

    /*
    Output:
    Output: (on PHP 5.4.12)
    Running 2 tests, 5000 times each...
    The 500 highest and lowest results will be disregarded.
  2. tyler-sommer created this gist Mar 30, 2013.
    101 changes: 101 additions & 0 deletions Benchmark.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,101 @@
    <?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);

    }
    }
    }
    38 changes: 38 additions & 0 deletions test.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    <?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:
    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
    */