当前位置: 首页 > news >正文

PHP高精度计时器与性能基准

PHP高精度计时器与性能基准

性能测试需要高精度计时。PHP的microtime和hrtime可以精确到微秒和纳秒。今天说说PHP中计时和性能基准测试。

microtime的使用。

```php
$start = microtime(true);
usleep(150000);
$end = microtime(true);

echo "耗时: " . round(($end - $start) * 1000, 3) . "ms\n";

// hrtime更高精度(PHP 7.3+)
$start = hrtime(true);
usleep(150000);
$end = hrtime(true);

echo "耗时: " . round(($end - $start) / 1e6, 3) . "ms\n";
?>

性能基准测试工具。

```php
class Benchmark
{
private array $results = [];

public function add(string $name, callable $fn, int $iterations = 1000): void
{
// 预热
$fn();

$start = hrtime(true);
for ($i = 0; $i < $iterations; $i++) {
$fn();
}
$end = hrtime(true);

$totalNs = $end - $start;
$avgNs = $totalNs / $iterations;

$this->results[] = [
'name' => $name,
'iterations' => $iterations,
'total_ms' => round($totalNs / 1e6, 3),
'avg_us' => round($avgNs / 1000, 3),
];
}

public function report(): string
{
$report = "基准测试结果\n" . str_repeat('-', 60) . "\n";
$report .= sprintf("%-25s %-15s %-15s\n", '测试', '总耗时(ms)', '平均(us)');
$report .= str_repeat('-', 60) . "\n";

foreach ($this->results as $r) {
$report .= sprintf("%-25s %-15s %-15s\n", $r['name'], $r['total_ms'], $r['avg_us']);
}

return $report;
}
}

$bench = new Benchmark();

$bench->add('array_push', function () {
$arr = [];
for ($i = 0; $i < 100; $i++) array_push($arr, $i);
}, 1000);

$bench->add('arr[]=', function () {
$arr = [];
for ($i = 0; $i < 100; $i++) $arr[] = $i;
}, 1000);

$bench->add('implode', function () {
implode(',', range(0, 99));
}, 1000);

echo $bench->report();
?>

性能对比测试。

```php
function benchmarkComparison(): void
{
$data = range(1, 10000);
$target = 9999;

// in_array
$start = hrtime(true);
for ($i = 0; $i < 100; $i++) in_array($target, $data);
echo "in_array: " . round((hrtime(true) - $start) / 1e6, 2) . "ms\n";

// array_flip + isset
$flipped = array_flip($data);
$start = hrtime(true);
for ($i = 0; $i < 100; $i++) isset($flipped[$target]);
echo "isset: " . round((hrtime(true) - $start) / 1e6, 2) . "ms\n";
}

benchmarkComparison();
?>

时间单位转换。

```php
function formatDuration(float $seconds): string
{
if ($seconds < 0.001) return round($seconds * 1000000, 2) . 'μs';
if ($seconds < 1) return round($seconds * 1000, 2) . 'ms';
if ($seconds < 60) return round($seconds, 2) . 's';
return floor($seconds / 60) . 'm ' . round($seconds % 60, 2) . 's';
}

echo formatDuration(0.00015) . "\n";
echo formatDuration(0.5) . "\n";
echo formatDuration(125.5) . "\n";
?>

PHP的高精度计时器在性能测试中很重要。microtime精确到微秒,hrtime精确到纳秒。基准测试要预热让OPcache生效,多次测试取平均值。对比测试选择合适的数据结构。

http://www.zskr.cn/news/1485074.html

相关文章:

  • 智慧农业AI+DeepSeek的病虫害检测与环境监测一体化智能云平台
  • 当无人机装上‘动态视觉神经’:事件相机在四旋翼避障与电力线巡检中的实战解析
  • 从零到精通:保姆级Illustrator 2024入门教程(附B站宝藏视频清单)
  • 别再复制粘贴了!手把手教你解析CMSIS-DAP下载算法里的神秘32字节头文件
  • 别再死记硬背TCP了!从RDT 1.0到3.0,手把手带你理解可靠传输的底层逻辑
  • 模板驱动型文档自动化:告别填空式写作的工程化实践
  • 2026年临沂三体系审核员外审员CCAA众智商学院报名资料试听课班期咨询官网400冯老师 - 众智商学院职业教育
  • MuleSoft+LLM企业级AI编排实战:安全、可治理的智能集成
  • 不止是输入框:用微信小程序input玩转搜索框、验证码和密码强度检测
  • PHP面向对象SOLID原则
  • 光子电路交换技术突破分布式ML通信瓶颈
  • 股票 / 基金理财业务落地成交易系统完整方案
  • 用STM32F103和W5500芯片,5分钟搞定一个Modbus-TCP从站(附完整代码)
  • 2026年福州物流仓储岗位SCMP班期怎么核对?众智商学院400冯老师费用资料 - 众智商学院官方
  • 别再死记硬背了!用Python模拟RDT协议(可靠数据传输)的发送与接收状态机
  • 跟着B站大佬复现Swin Transformer图像分类:从PyTorch代码到花卉数据集实战(附完整代码)
  • 别再只看FLOPs了!ShuffleNet v2作者教你用4条黄金法则设计真正高效的移动端网络
  • Sqribble文档操作系统:模板驱动的PDF自动化生成原理与实践
  • 在线污泥浓度计十大优选品牌深度解析——从核心技术到工程实战的全维度选型指南 - 仪表品牌榜
  • ESP32+LVGL实战:用ST7789和ILI9341屏幕跑个音乐播放器Demo(ESP-IDF环境)
  • 炉石传说HsMod插件终极指南:55项隐藏功能全面解锁
  • Gemini CLI:终端原生的免费AI编程助手
  • MyBatis-Plus IService 封装完全指南
  • VS Code 数据科学协作工程化:从 Notebook 到可复现团队工作流
  • 拆解一个Type-C扩展坞:看PS176芯片如何实现4K 60Hz视频转换
  • VMware解锁工具深度解析:3步实现macOS虚拟机跨平台运行
  • 3D-LLM:大语言模型原生理解三维空间与工程制造
  • Django REST项目一键生成OpenAPI 3文档的轻量级工具,支持装饰器精细标注与多场景扩展
  • 保姆级教程:在威联通NAS上用Docker搞定qBittorrent到Transmission的自动转种与辅种
  • 前端直接生成带格式Excel:字体、行列宽、合并单元格全搞定