GetLocalTime()API时间解析 [英] GetLocalTime() API time resolution

查看:566
本文介绍了GetLocalTime()API时间解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找出我的应用程序中某个函数所花费的时间.应用程序是MS VIsual Studio 2005解决方案,所有C代码.

I need to find out time taken by a function in my application. Application is a MS VIsual Studio 2005 solution, all C code.

我使用Windows API GetLocalTime(SYSTEMTIME *)来获取要测量时间的函数调用前后的当前系统时间. 但这具有最低分辨率仅为1毫秒的缺点.没有什么比这更重要的了.因此,我无法在微秒内获得任何时间粒度.

I used thw windows API GetLocalTime(SYSTEMTIME *) to get the current system time before and after the function call which I want to measure time of. But this has shortcoming that it lowest resolution is only 1msec. Nothing below that. So I cannot get any time granularity in micro seconds.

我知道time()给出自纪元时间起经过的时间,它的分辨率也为1毫秒(无微秒)

I know that time() which gives the time elapsed since the epoch time, also has resolution of 1msec (No microseconds)

1.)还有其他Windows API可以以微秒为单位给出时间,我可以用它来衡量函数消耗的时间吗?

1.) Is there any other Windows API which gives time in microseconds which I can use to measure the time consumed by my function?

-AD

推荐答案

还有其他可能性.

QueryPerformanceCounter 将返回一个性能计数器",它实际上是一个由CPU管理的64位计数器,从计算机加电开始,从0开始递增.此计数器的频率由 QueryPerformanceFrequency 返回.要获取中的时间参考,请将性能计数器除以性能频率.在Delphi中:

QueryPerformanceCounter will return a "performance counter" which is actually a CPU-managed 64-bit counter that increments from 0 starting with the computer power-on. The frequency of this counter is returned by the QueryPerformanceFrequency. To get the time reference in seconds, divide performance counter by performance frequency. In Delphi:

function QueryPerfCounterAsUS: int64;     
begin
  if QueryPerformanceCounter(Result) and
     QueryPerformanceFrequency(perfFreq)
  then
    Result := Round(Result / perfFreq * 1000000);
  else
    Result := 0;
end;

在多处理器平台上,无论线程当前在哪个CPU上运行,QueryPerformanceCounter 都应返回一致的结果.但是,偶尔会出现问题,通常是由硬件芯片或BIOS中的错误引起的.通常,补丁是由主板制造商提供的.来自MSDN的两个示例:

On multiprocessor platforms, QueryPerformanceCounter should return consistent results regardless of the CPU the thread is currently running on. There are occasional problems, though, usually caused by bugs in hardware chips or BIOSes. Usually, patches are provided by motherboard manufacturers. Two examples from the MSDN:

  • Programs that use the QueryPerformanceCounter function may perform poorly in Windows Server 2003 and in Windows XP
  • Performance counter value may unexpectedly leap forward

QueryPerformanceCounter的另一个问题是运行速度很慢.

Another problem with QueryPerformanceCounter is that it is quite slow.

如果可以将代码限制为一个CPU(SetThreadAffinity),则可以使用 RDTSC 汇编程序直接从处理器查询性能计数器的指令.

If you can limit your code to one CPU (SetThreadAffinity), you can use RDTSC assembler instruction to query performance counter directly from the processor.

function CPUGetTick: int64;
asm
  dw 310Fh // rdtsc
end;

RDTSC结果以与QueryPerformanceCounter相同的频率递增.将其除以QueryPerformanceFrequency即可获得以秒为单位的时间.

RDTSC result is incremented with same frequency as QueryPerformanceCounter. Divide it by QueryPerformanceFrequency to get time in seconds.

QueryPerformanceCounter比RDTSC要慢得多,因为它必须考虑多个CPU和频率可变的CPU.摘自 Raymon Chen的博客:

QueryPerformanceCounter is much slower thatn RDTSC because it must take into account multiple CPUs and CPUs with variable frequency. From Raymon Chen's blog:

(QueryPerformanceCounter)计算经过的时间.它必须,因为它的价值是 由QueryPerformanceFrequency函数控制,该函数返回一个数字 指定每秒的单位数,并且将频率指定为不 在系统运行时进行更改.

(QueryPerformanceCounter) counts elapsed time. It has to, since its value is governed by the QueryPerformanceFrequency function, which returns a number specifying the number of units per second, and the frequency is spec'd as not changing while the system is running.

对于可以变速运行的CPU,这意味着HAL不能 请使用RDTSC之类的指令,因为该指令与经过时间无关.

For CPUs that can run at variable speed, this means that the HAL cannot use an instruction like RDTSC, since that does not correlate with elapsed time.

timeGetTime

TimeGetTime 属于Win32多媒体Win32函数.至少在现代硬件上,它以1 ms的分辨率返回以毫秒为单位的时间.完成后,如果在开始测量time和timeEndPeriod(1)之前运行timeBeginPeriod(1)并没有什么坏处.

timeGetTime

TimeGetTime belongs to the Win32 multimedia Win32 functions. It returns time in milliseconds with 1 ms resolution, at least on a modern hardware. It doesn't hurt if you run timeBeginPeriod(1) before you start measuring time and timeEndPeriod(1) when you're done.

在Vista之前, GetLocalTime 精度与准确性不同) 在Vista上,GetLocalTime和GetSystemTime都可以使用1毫秒的分辨率.

Before Vista, both GetLocalTime and GetSystemTime return current time with millisecond precision, but they are not accurate to a millisecond. Their accuracy is typically in the range of 10 to 55 milliseconds. (Precision is not the same as accuracy) On Vista, GetLocalTime and GetSystemTime both work with 1 ms resolution.

这篇关于GetLocalTime()API时间解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆