macocci7 / php-benchmark
PHP的简单基准测试脚本。
0.2.1
2024-04-18 01:34 UTC
Requires (Dev)
- php-parallel-lint/php-parallel-lint: ^1.3
- phpmd/phpmd: ^2.15
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.5
- squizlabs/php_codesniffer: 3.*
README
一个简单的PHP基准测试脚本。
1. 内容
2. 要求
- PHP 8.1 或更高版本
- (可选) git
- (可选) Composer
3. 安装
有几种选项
-
选项1:将Benchmark.php复制或下载到您的环境中
并在您的PHP中
require
代码,而不是vendor/autoload.php
。这是最简单的方法。
-
选项2:将此存储库克隆到您的环境中
git clone https://github.com/macocci7/PHP-Benchmark.git
-
选项3:从GitHub存储库下载ZIP
点击[代码]按钮,然后点击[下载ZIP]。
然后,解压ZIP并将
src/Benchmark.php
复制到您的环境中。 -
选项4:使用Composer
composer require macocci7/php-benchmark
4. 使用
4.1. 使用:运行单个代码
-
PHP
<?php require_once __DIR__ . '/../vendor/autoload.php'; use Macocci7\PhpBenchmark\Benchmark; $iteration = 10000; $haystack = 'GPSAltitude'; $needle = 'GPS'; $name = 'str_starts_with()'; $callback = function ($haystack, $needle) { if (str_starts_with($haystack, $needle)) { } }; $params = [ 'haystack' => $haystack, 'needle' => $needle, ]; $result = Benchmark::code($name, $callback, $params, $iteration); Benchmark::stdout($result);
-
结果
1: str_starts_with() => Time: 0.008332 sec
4.2. 使用:运行多个代码
-
PHP
<?php require_once __DIR__ . '/../vendor/autoload.php'; use Macocci7\PhpBenchmark\Benchmark; $iteration = 10000; $params = [ 'haystack' => 'GPSAltitude', 'needle' => 'GPS', 'pattern' => sprintf("/^%s/", 'GPS'), ]; $sort = true; $desc = false; $callbacks = [ 'str_starts_with()' => function ($haystack, $needle, $pattern) { if (str_starts_with($haystack, $needle)) { } }, 'strpos()' => function ($haystack, $needle, $pattern) { if (strpos($haystack, $needle)) { } }, 'strpbrk()' => function ($haystack, $needle, $pattern) { if (strpbrk($haystack, $needle)) { } }, 'strncmp()' => function ($haystack, $needle, $pattern) { if (0 === strncmp($haystack, $needle, 3)) { } }, 'strstr()' => function ($haystack, $needle, $pattern) { if (strstr($haystack, $needle)) { } }, 'preg_match()' => function ($haystack, $needle, $pattern) { if (preg_match($pattern, $haystack)) { } }, 'strcmp() + substr()' => function ($haystack, $needle, $pattern) { if (0 === strcmp(substr($haystack, 0, 3), $needle)) { } }, 'substr_compare()' => function ($haystack, $needle, $pattern) { if (0 === substr_compare($haystack, $needle, 0, 3)) { } }, ]; $results = Benchmark::codes($callbacks, $params, $iteration, $sort, $desc); Benchmark::stdout($results);
-
结果
1: str_starts_with() => Time: 0.008634 sec 2: strpbrk() => Time: 0.008911 sec 3: strncmp() => Time: 0.009266 sec 4: strstr() => Time: 0.009594 sec 5: strpos() => Time: 0.009826 sec 6: preg_match() => Time: 0.011233 sec 7: substr_compare() => Time: 0.011373 sec 8: strcmp() + substr() => Time: 0.012719 sec
5. 示例
- RunSingleCode.php >> 在RunSingleCode.txt中查看结果
- RunMultipleCodes.php >> 在RunMultipleCodes.txt中查看结果
6. LICENSE
文档创建日期:2024/01/09
文档更新日期:2024/04/18