alexgaal / php-benchmark
一个简单的PHP基准测试类
v0.0.2
2017-05-04 14:46 UTC
Requires
- php: >=7.0
This package is auto-updated.
Last update: 2024-08-30 22:14:40 UTC
README
一个简单的PHP基准测试类。
使用示例
基准测试一个函数
<?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);
基准测试一段代码块
<?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 $calculateBenchmark = Benchmark::begin(); for ($i = 0; $i < 1000; $i++) { pow($i, $i); } $databaseBenchmark = Benchmark::begin(); // do some random database stuff $databaseBenchmark->stop(); $calculateBenchmark->stop();
函数签名
$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)