高分辨率计时器/代码运行时间->高架? [英] High resolution timer/code run time -> overhead?

查看:50
本文介绍了高分辨率计时器/代码运行时间->高架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用高分辨率计时器来查找我的代码运行时间,我注意到计时器的结果不一致,我想找出原因.

I'm trying to find my Code run time using a high resolution timer, I've noticed that the timer has inconsistent results and I would like to find out why this is.

我找到了这篇文章如何测试VBA代码的运行时间?
并且已经实现了最佳答案,我尝试使用它来查找多个函数的运行时间,并注意到结果发生了很大变化.

I found this article How do you test running time of VBA code?
and have implemented the top answer, I tried using it to find the run time of several functions and noticed that the results changed fairly drastically.

要查看这是否是计时器故障,我做了一个函数,可以启动和停止计时器.

To see if this was a fault of the timer, i made a function where I just started and stopped the timer.

Public Sub test_ctimer()
    Dim results(0 To 4) As Double
    Dim t As CTimer: Set t = New CTimer
    Dim i As Integer

    'Removes msg box overhead
    MsgBox "about to start"

    For i = 0 To 4
        t.StartCounter
        results(i) = t.TimeElapsed

    Next i

    For i = 0 To 4
        MsgBox results(i)
    Next i
End Sub

第一次测量要比随后的任何测量花费更多的时间(〜1个数量级).有人知道为什么吗?

The first measurement takes significantly more time (~ 1 magnitude greater) than any of the following measurements. Does anyone know why this is?

下面是>如何测试运行情况下的ctimer代码VBA代码的时间?

Option Explicit

Private Type LARGE_INTEGER
    lowpart As Long
    highpart As Long
End Type

Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As LARGE_INTEGER) As Long
Private Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As LARGE_INTEGER) As Long

Private m_CounterStart As LARGE_INTEGER
Private m_CounterEnd As LARGE_INTEGER
Private m_crFrequency As Double

Private Const TWO_32 = 4294967296# ' = 256# * 256# * 256# * 256#

Private Function LI2Double(LI As LARGE_INTEGER) As Double
Dim Low As Double
    Low = LI.lowpart
    If Low < 0 Then
        Low = Low + TWO_32
    End If
    LI2Double = LI.highpart * TWO_32 + Low
End Function

Private Sub Class_Initialize()
Dim PerfFrequency As LARGE_INTEGER
    QueryPerformanceFrequency PerfFrequency
    m_crFrequency = LI2Double(PerfFrequency)
End Sub

Public Sub StartCounter()
    QueryPerformanceCounter m_CounterStart
End Sub

Property Get TimeElapsed() As Double
Dim crStart As Double
Dim crStop As Double
    QueryPerformanceCounter m_CounterEnd
    crStart = LI2Double(m_CounterStart)
    crStop = LI2Double(m_CounterEnd)
    TimeElapsed = 1000# * (crStop - crStart) / m_crFrequency
End Property

推荐答案

链接的类不是很好.调用QueryPerformanceCounter会产生开销,应该首先确定开销,然后将其用于调整后续调用.这可以解释所有结果都比预期的慢一点但是不能解释第一个结果要慢得多.无论如何,请参见 http://support.microsoft.com/kb/172338 描述了一种调整方法为了它.我目前无法访问已实施的课程,因此无法发布相关部分.

The linked class is not great. There is an overhead associated with calling QueryPerformanceCounter it and that overhead should be determined first then used to adjust the subsequent calls. This would explain all results being slightly slower than expected, however it does not explain the first one being substantially slower. Anyway, see http://support.microsoft.com/kb/172338 describes an approach to adjust for it. The class I have implemented I do not have access to at this time so I cannot post the relevant parts.

也可以阅读与您的特定问题无关的内容,但通常与Windows上的Timings有关,

Also for a read unrelated to your specific issue, but related to Timings on Windows in general, http://msdn.microsoft.com/en-gb/library/windows/desktop/dn553408%28v=vs.85%29.aspx is interesting.

同时,要解决此问题,请添加一个调用以开始,然后在Class_Initialize中停止.

As a workaround in the meantime, add a call to start then stop in the Class_Initialize.

这根本不是一个答案-只是一个扩展注释-因此,如果我找到一个确凿的理由,我将对其进行编辑;)

This is not really an answer at all - more of an extended comment - so I will edit it if I ever find a conclusive reason ;)

再次重申:我无法解释OP查询结果的原因.我假设这与VBA解释器(即接近JIT'er)有关,但我无法证明这两种方式.我上面的评论与OP正在使用的功能有关,但可能不合逻辑.

To reiterate to those upvoting: I cannot explain the reason for the results the OP is querying. I hypothesise it is to do with the VBA interpreter (i.e. close to a JIT'er) but I cannot evidence either way. My commentary above is related to the functions the OP is using but are probably non sequitur.

这篇关于高分辨率计时器/代码运行时间-&gt;高架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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