BenchMarker
Benchmark running times of PHP code (inspired by Benchmark.pm), (*1)
Synopsis
$benchmarker = new \Moznion\Benchmarker("all");
$code = function () use ($foo) {
// do something
};
$benchmarker->timeThese(10000, [
"code A" => function () use ($bar) {
// do something
},
"code B" => $code, // it can also take variable of anonymous function
]);
// Sample of Output:
// code A: 3.9326 wallclock secs (3sr + 0.2562ys = PU)
// code B: 6.40877 wallclock secs (6sr + 0.81337ys = PU)
$benchmarker->cmpThese(10000, [
"code A" => function () use ($bar) {
// do something
},
"code B" => $code, // it can also take variable of anonymous function
]);
// Sample of Output:
// Rate code A code B
// code A 22571/s -- -49%
// code B 44656/s 98% --
Description
Library for benchmarking the running times of PHP code., (*2)
This library is inspired by Benchmark of Perl, (*3)
Methods
Creates the instance of BenchMarker., (*4)
Specify the name of style. Default value is auto., (*5)
Please see also Style., (*6)
Specify the format to format the values of benchmark's result.
It supports the format which is compatible with sprintf()'s one., (*7)
Default value is '5.2f'., (*8)
Specify cache the elapsed time of nop function (elapsed time of nop is used internal for accuracy).
If you enable caching, processing time of benchmark will be faster., (*9)
Default value is 'false'., (*10)
timeIt($count, $code)
Run a chunk of code and see how long it goes., (*11)
This method returns instance of result-time., (*12)
$count is the number of times to run the loop. This argument must be positive integer., (*13)
$code is the code to run. This argument must be callable variable., (*14)
countIt($time, $code)
See how many times a chunk of code runs in a given time., (*15)
This method returns instance of result-time., (*16)
$time is the minimum length of time to run the loop., (*17)
$code is the code to run. This argument must be callable variable., (*18)
timeThis($count, $code, $title=null)
Run a chunk of code several times and print result of benchmark., (*19)
This method returns instance of result-time., (*20)
$count is the number of times to run the loop. This argument must be positive integer., (*21)
This argument can be zero or negative.
This means the minimum number of CPU seconds to run.
A zero signifies the default of 3 seconds., (*22)
$code is the code to run. This argument must be callable variable., (*23)
Title of result. $title defaults to "timethis $count"., (*24)
timeThese($count, $codes)
Run several chunks of code several times and print result of benchmark., (*25)
This method returns instance of result-time of array for each codes., (*26)
$count is the number of times to run the loop. This argument must be positive integer., (*27)
This argument can be zero or negative.
This means the minimum number of CPU seconds to run.
A zero signifies the default of 3 seconds., (*28)
$codes is the array of codes to run., (*29)
cmpThese($count, $codes)
Print results of timeThese as a comparison chart., (*30)
$count is the number of times to run the loop. This argument must be positive integer., (*31)
This argument can be zero or negative.
This means the minimum number of CPU seconds to run.
A zero signifies the default of 3 seconds., (*32)
$codes is the array of codes to run., (*33)
timeStr($time_result, $count=null)
Print formatted time., (*34)
instance of result-time., (*35)
Number of loops. If this argument is not null then this method appends the rate., (*36)
timeDiff($new_time, $old_time)
Calculates difference between $new_time and $old_time., (*37)
Arguments must be instance of result-time., (*38)
clearCache($count)
Clear the caching of nop function which is related $count., (*39)
clearAllCache()
Clear all of the caching of nop function., (*40)
disableCache()
Disable to cache the caching of nop function., (*41)
enableCache()
Enable to cache the caching of nop function., (*42)
Result Time
Time class has 6 public members., (*43)
Real duration., (*44)
Duration of system time., (*45)
Duration of user time., (*46)
Duration of system time of child processes., (*47)
Duration of user time of child processes., (*48)
Times of loop to run., (*49)
Style
This library supports 5 styles., (*50)
Output all of the result of benchmarking., (*51)
Output the result of benchmarking without child processes result., (*52)
Output the result of benchmarking without parent processes result., (*53)
Output nothing., (*54)
If child processes CPU time is bigger than 0 then this style is the same as "all". Elsewise, this style is the same as "noc"., (*55)
Requires
PHP (version 5.4 or later), (*56)
See Also
Notes
This library doesn't work on Microsoft Windows., (*57)
License
MIT, (*58)