dev-master
9999999-devYet another micro-framework for timing PHP code
MIT
The Requires
- php >=5.5.0
The Development Requires
by Alexander Wühr
Wallogit.com
2017 © Pedro Peláez
Yet another micro-framework for timing PHP code
¡Ándale! ¡Ándale! ¡Arriba! ¡Arriba! ¡Epa! ¡Epa! ¡Epa! Yeehaw!, (*1)
"Gosh, yet another micro-framework for timing PHP code.." you might say. I can deal with that. Almost., (*2)
$ composer require "lx/gonzalez" "dev-master"
<?php use \Gonzalez\Runner require_once 'vendor/autoload.php'; $function_to_benchmark = function() { // code to benchmark goes here... }; $runner = new Runner($function_to_benchmark);
$results = $runner->run(1000); $iterations = count($results); $overall_time = array_sum($results); $fastest = min($results); $slowest = max($results); $average = $iterations / $overall_time;
Let's say you want to measure how fast you can drink a pint of beer. Let's also say you have only one arm. The steps it would take are:, (*3)
Start in the stopwatch app in your shiny smartphoneStop in the stopwatch app in your shiny smartphoneWhat you wanted to measure was only action #4, what you actually measured was action #2 to #7. Not good., (*4)
Gonzalez provides a simple way to calibrate your benchmarks: Gonzalez., (*5)
The minimum calibration you want to do is the time it takes php entering and leaving the function to benchmark. So if you want to benchmark an anonymous function you would take an empty anonymous function:, (*6)
$empty_anonymous_function = function () {};
$calibrator = new Runner($empty_anonymous_function);
$result = $calibrator->run(10000);
$calibration_time = min($result);
$runner->setCalibrationTime($calibration_time);
$result = $runner->run(1000);
// ...
In general your calibration function should be exactly the same as the function you want to benchmark, excluding the lines of code you really want to benchmark., (*7)
Yet another micro-framework for timing PHP code
MIT