You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

UEFI环境下计算程序运行时长始终为0秒的问题排查

UEFI应用计时结果为0秒的解决思路

1. 检查计时区间是否过短

你的代码里StartTick和EndTick是连续调用的,中间没有实际要测试的耗时逻辑,两者的时间差极有可能为0(甚至CPU在同一个周期内完成两次调用)。

  • 解决:把需要测试的耗时操作放在两次计数器调用之间,示例:
    Frequency = GetPerformanceCounterProperties(NULL, NULL);
    StartTick = GetPerformanceCounter();
    // 插入你要测试的UEFI操作,比如文件读写、复杂内存计算等
    YourTimeConsumingOperation();
    EndTick = GetPerformanceCounter();
    ElapsedTicks = EndTick > StartTick ? EndTick - StartTick : StartTick - EndTick;
    TimeInSeconds = (double)ElapsedTicks / Frequency;
    

2. 验证Performance Counter的有效性

  • 先确认GetPerformanceCounterProperties返回的Frequency是否合理:部分平台可能不支持高性能计数器,或者返回频率为0。可以打印Frequency的值排查。
  • 利用函数的两个参数获取更多计数器属性,判断其是否可用:
    UINT64 MinPeriod, MaxPeriod;
    Frequency = GetPerformanceCounterProperties(&MinPeriod, &MaxPeriod);
    // 打印Frequency、MinPeriod、MaxPeriod,确认计数器正常工作
    

3. 优化计数器回绕处理

虽然你做了基础的回绕判断,但可以用无符号整数减法简化逻辑(自动处理回绕):

ElapsedTicks = (UINT64)(EndTick - StartTick);

4. 尝试TimerLib替代接口

如果PerformanceLib兼容性不佳,可切换到TimerLib的定时器接口,兼容性通常更好:

UINT64 TimerFreq = GetTimerFrequency();
UINT64 StartTimer = GetTimerTick();
YourTimeConsumingOperation();
UINT64 EndTimer = GetTimerTick();
UINT64 ElapsedTimerTicks = EndTimer - StartTimer;
double TimeInSeconds = (double)ElapsedTimerTicks / TimerFreq;

5. 放大测试时长

如果目标操作本身耗时极短,可循环执行多次后计算平均耗时:

#define LOOP_COUNT 10000
Frequency = GetPerformanceCounterProperties(NULL, NULL);
StartTick = GetPerformanceCounter();
for (INT32 i = 0; i < LOOP_COUNT; i++) {
  YourShortOperation();
}
EndTick = GetPerformanceCounter();
ElapsedTicks = EndTick - StartTick;
double TotalTime = (double)ElapsedTicks / Frequency;
double SingleOperationTime = TotalTime / LOOP_COUNT;

内容的提问来源于stack exchange,提问作者Sharingan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 11:42:09