High Res VBA Timer

warhead92100

New Member
Joined
Jul 24, 2013
Messages
48
Hi Guys,

I've trying to review how long does my code run, however I would like it to be precise as much as possible. While googling a work around, I stumbled upon these class module code:

Code:
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

From: optimization - How do you test running time of VBA code? - Stack Overflow

However I quite don't get how this works, I'm trying to use it:


Code:
Public Sub setup()
    Dim t As Long
    
    With New CTimer
    .StartCounter

'codes....

.TimeElapsed
    t = .TimeElapsed
    
    End With
    
    MsgBox t
End Sub

But I'm getting an Invalid use of property error, sorry I'm still pretty new to VBA, can someone spell this out for me.

Thanks!
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Remove the
Code:
.TimeElapsed
line - you need to assign the result of the property to a variable (as you do in the next line).
 
Upvote 0
Code:
Public Sub setup()
    Dim t As Long
    
    With New CTimer
    .StartCounter


'codes....


    '.TimeElapsed Remove this line
    t = .TimeElapsed
    
    End With
    
    MsgBox t
End Sub

Simply remove the line indicated, time elapsed returns a number, Excel doesn't know what to do with it
 
Upvote 0
I've trying to review how long does my code run, however I would like it to be precise as much as possible. While googling a work around, I stumbled upon these class module code:
[....]
From: optimization - How do you test running time of VBA code? - Stack Overflow

That's a poor implementation, IMHO. I think the implementation below is cleaner, and it might be easier to understand.

Also note that in your usage, it is better to declare t As Double instead of Long. That retains the full precision of TimeElapsed, which is even better than microseconds. (But I think it is sufficient to format only to the microsecond, 3 decimal places.)
Code:
Option Explicit
Private Declare Function QueryPerformanceCounter Lib "kernel32" _
    (lpPerformanceCount As Currency) As Long
Private Declare Function QueryPerformanceFrequency Lib "kernel32" _
    (lpFrequency As Currency) As Long

Private m_CounterStart As Currency
Private m_CounterEnd As Currency
Private d_PerfFrequency As Double

Private Sub Class_Initialize()
    Dim m_PerfFrequency As Currency
    QueryPerformanceFrequency m_PerfFrequency
    d_PerfFrequency = m_PerfFrequency
End Sub

Public Sub StartCounter()
    QueryPerformanceCounter m_CounterStart
End Sub

Property Get TimeElapsed() As Double    ' milliseconds
    QueryPerformanceCounter m_CounterEnd
    TimeElapsed = 1000# * (m_CounterEnd - m_CounterStart) / d_PerfFrequency
End Property
 
Upvote 0

Forum statistics

Threads
1,215,221
Messages
6,123,699
Members
449,117
Latest member
Aaagu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top