Tool to compare different functions in PHP

v1.4 2014-03-22 07:38 UTC


Tool to compare different functions in PHP


Via Composer

composer require lavoiesl/php-benchmark


$benchmark = new \Lavoiesl\PhpBenchmark\Benchmark();

$benchmark->add('md5',   function() { return md5('test');   });
$benchmark->add('sha1',  function() { return sha1('test');  });


You can run $benchmark->run(false) to get results without any output

Notes about memory usage

Memory usage is monitored using register_tick_function but this does not do a good job at analysing small statements since the memory gets cleaned too quickly.

A simple trick is the return the value, the AbstractTest stores it temporarily.

To ensure proper tick analysis, use declare(ticks = 1); as early as possible.

See the memory test.


Running tests 3000 times.
Testing 2/2 : sha1

Test       Time   Time (%)   Memory   Memory (%)
md5     1304 ms                 0 B
sha1    2077 ms       59 %      0 B

Optimal test count guessing

By default, Benchmark will try to find an optimal number of runs so that each test takes a maximum of 2 seconds.

You can change this by forcing it with $benchmark->setCount($n) or change the time with $benchmark->guessCount($max_seconds).

Writing custom tests

You can extend AbstractTest and provide your own wrapper.

For an example of this, see the command test and the corresponding class.

A full example can be seen here: