PHP library that allows you benchmark and compare the performance of functions
MIT License
PHP library that allows you benchmark and compare the performance of functions.
0.0.1
To get started, use Composer to add the package to your project's dependencies:
$ composer require mookofe/php-benchmark
function bubbleSort(array $array): void
{
if (!$length = count($array)) {
return $array;
}
for ($outer = 0; $outer < $length; $outer++) {
for ($inner = 0; $inner < $length; $inner++) {
if ($array[$outer] < $array[$inner]) {
$tmp = $array[$outer];
$array[$outer] = $array[$inner];
$array[$inner] = $tmp;
}
}
}
}
function quickSort(array $array): void
{
if (!$length = count($array)) {
return $array;
}
$k = $array[0];
$x = $y = array();
for ($i=1;$i<$length;$i++) {
if ($array[$i] <= $k) {
$x[] = $array[$i];
} else {
$y[] = $array[$i];
}
}
return array_merge(quickSort($x),array($k),quickSort($y));
}
Given these two functions, let's benchmark them
use Mookofe\Benchmark\Method;
use Mookofe\Benchmark\Sorters\Min;
use Mookofe\Benchmark\Orchestrator;
use Mookofe\Benchmark\Sorters\Order\Asc;
use Mookofe\Benchmark\Repositories\IOReporter;
...
//Define orchestrator
$orchestrator = Orchestrator();
/** Add paraters to test */
$orchestrator->addParameters([5, 4, 3, 2, 1]);
$orchestrator->addParameters([100, 5, 300]);
$orchestrator->addParameters([20, 10, 9, 25]);
/** Add methods */
$orchestrator->addMethod(new Method('bubbleSort'));
$orchestrator->addMethod(new Method('quickSort'));
/** Run tests 10 times */
$results = $orchestrator->run(10);
/** Run reporter */
$reporter = new IOReporter($results);
$reporter->setPath('results.txt');
//Sorter
$asc = new Asc();
$sorter = Min($asc);
/** Generate report */
$reporter->generate($sorter);
Results:
********************************************************************************
Benchmark Report
********************************************************************************
Running times: 10
Number of functions: 2
Number of parameters set: 3
SUMMARY:
Function Parameters Min Max Avg Median
--------------------------------------------------------------------------------
bubbleSort ([100, 5, 300]) 3.0994 5.0068 3.0994 3.0994
bubbleSort ([20, 10, 9, 25]) 5.0068 28.8486 5.0068 5.0068
bubbleSort ([5, 4, 3, 2, 1]) 6.9141 22.8882 6.9141 6.9141
quickSort ([100, 5, 300]) 10.0136 11.9209 10.0136 10.0136
quickSort ([5, 4, 3, 2, 1]) 10.9673 377.8934 10.9673 10.9673
quickSort ([20, 10, 9, 25]) 13.113 19.0735 13.113 13.113
* Times in microsecond (ยตs)
Sort the summary report by the min field
use Mookofe\Benchmark\Sorters\Min;
use Mookofe\Benchmark\Sorters\Order\Desc;
...
/** Sorter */
$desc = new Desc();
$sorter = new Min($desc);
/** Generate report */
$reporter->generate($sorter);
Sort the summary report by the max field
use Mookofe\Benchmark\Sorters\Max;
use Mookofe\Benchmark\Sorters\Order\Asc;
...
/** Sorter */
$asc = new Asc();
$sorter = new Max($asc);
Sort the summary report by the avg field
use Mookofe\Benchmark\Sorters\Max;
use Mookofe\Benchmark\Sorters\Order\Avg;
...
$asc = Asc();
$sorter = new Avg($asc);
Sort the summary report by the median field
use Mookofe\Benchmark\Sorters\Max;
use Mookofe\Benchmark\Sorters\Order\Median;
...
$asc = new Asc();
$sorter = new Median($asc);
use Mookofe\Benchmark\Sorters\Median;
use Mookofe\Benchmark\Sorters\Order\Asc;
use Mookofe\Benchmark\Filters\FunctionName;
...
$asc = new Asc();
$sorter = new Median($asc);
/** Filters */
$functionNames = [
'bubbleSort'
];
$functionNameFilter = new FunctionName($functionNames);
$reporter->addFilter($functionNameFilter);
/** Generate report */
$reporter->generate($sorter);
use Mookofe\Benchmark\Sorters\Median;
use Mookofe\Benchmark\Sorters\Order\Asc;
use Mookofe\Benchmark\Filters\Parameter;
...
$asc = new Asc();
$sorter = Median($asc);
/** Filters */
$parametersFilter = new Parameter();
$parametersFilter->addSet([5, 4, 3, 2, 1]);
$parametersFilter->addSet([100, 5, 300]);
$reporter->addFilter($parametersFilter);
/** Generate report */
$reporter->generate($sorter);
This package is open-sourced software licensed under the MIT license