alexgaal/php-benchmark

A simple PHP Benchmark class

v0.0.2 2017-05-04 14:46 UTC

This package is auto-updated.

Last update: 2024-11-30 22:48:21 UTC


README

A simple PHP Benchmark class.

Example usage

Benchmark a function

<?php
$forBenchmark = Benchmark::time(function () {
    for ($i = 0; $i < 100; $i++) {
        //
    }
});

$whileBenchmark = Benchmark::time(function () {
    $i = 0;
    while ($i < 100) {
        $i++;
    }
});

echo $forBenchmark->compare($whileBenchmark);

Benchmark a block of code

<?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);

Nested benchmarks

<?php
$calculateBenchmark = Benchmark::begin();
for ($i = 0; $i < 1000; $i++) {
    pow($i, $i);
}

$databaseBenchmark = Benchmark::begin();
// do some random database stuff
$databaseBenchmark->stop();
$calculateBenchmark->stop();

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)