Wallogit.com
2017 © Pedro Peláez
A simple PHP Benchmark class
A simple PHP Benchmark class., (*1)
``` php <?php $forBenchmark = Benchmark::time(function () { for ($i = 0; $i < 100; $i++) { // } });, (*2)
$whileBenchmark = Benchmark::time(function () { $i = 0; while ($i < 100) { $i++; } });, (*3)
echo $forBenchmark->compare($whileBenchmark);, (*4)
### Benchmark a block of code ``` php <?php $forBenchmark = Benchmark::begin(); for ($i = 0; $i < 100; $i++) { // } $forBenchmark->stop(); $whileBenchmark = Benchmark::begin(); $i = 0; while ($i < 100) { $i++; } $whileBenchmark->stop(); echo $forBenchmark->compare($whileBenchmark);
``` php <?php $calculateBenchmark = Benchmark::begin(); for ($i = 0; $i < 1000; $i++) { pow($i, $i); }, (*5)
$databaseBenchmark = Benchmark::begin(); // do some random database stuff $databaseBenchmark->stop(); $calculateBenchmark->stop();, (*6)
## Function signature
$callback Closure A function which will be called in Benchmark::time() function. $iterations int Number of iterations of function call. $avg bool Returns average values of memory and time if true, otherwise will return accumulated values. Benchmark::time(\Closure $callback, int $iterations, bool $avg) ```, (*7)