nice / bench
一个简单的PHP基准测试框架
1.0.1
2014-11-23 16:17 UTC
Requires
- php: >=5.3.3
Requires (Dev)
- phpunit/phpunit: ~3.7
This package is not auto-updated.
Last update: 2024-09-24 04:09:55 UTC
README
一个简单的PHP基准测试,适用于日常微优化器。
<?php require __DIR__ . '/vendor/autoload.php'; use Nice\Benchmark\Benchmark; $benchmark = new Benchmark(100000, 'String matching'); $benchmark->register('preg_match', function() { assert(preg_match('/^test/', 'testing')); }); $benchmark->register('strpos', function() { assert(strpos('testing', 'test') === 0); }); $benchmark->execute();
Running "String matching" consisting of 2 tests, 100,000 iterations...
Values that fall outside of 3 standard deviations of the mean are discarded.
For preg_match out of 98,997 runs, average time was 0.0000118057 seconds.
For strpos out of 99,871 runs, average time was 0.0000104146 seconds.
Results:
Test Name Time + Interval Change
strpos 0.0000104146 +0.0000000000 baseline
preg_match 0.0000118057 +0.0000013912 13% slower
安装
推荐通过 Composer 安装 Nice Bench。只需在项目目录中运行 php composer.phar require
命令即可安装
php composer.phar require nice/bench:~1.0
用法
在项目目录中创建一个名为 tests.php
的文件,并添加以下内容:
<?php require __DIR__ . '/vendor/autoload.php'; use Nice\Benchmark\Benchmark; $arr = range(1, 10000); $benchmark = new Benchmark(10000, 'foreach'); $benchmark->register('foreach with value', function() use ($arr) { foreach ($arr as $value) { } }); $benchmark->register('foreach with key, value', function() use ($arr) { foreach ($arr as $key => $value) { } }); $benchmark->execute();
然后在终端中运行 tests.php
$ php tests.php Running "foreach" consisting of 2 tests, 10,000 iterations... Values that fall outside of 3 standard deviations of the mean are discarded. For foreach with value out of 9,871 runs, average time was 0.0011523914 seconds. For foreach with key, value out of 9,874 runs, average time was 0.0016814657 seconds. Results: Test Name Time + Interval Change foreach with value 0.0011523914 +0.0000000000 baseline foreach with key, value 0.0016814657 +0.0005290744 46% slower
有关另一个示例,请查看 example.php。