Is there code in VBA I can wrap a function with that will let me know the time it took to run, so that I can compare the different running times of functions?
Join Stack Overflow to learn, share knowledge, and build your career.
Unless your functions are very slow, you're going to need a very high-resolution timer. The most accurate one I know is QueryPerformanceCounter. Google it for more info. Try pushing the following into a class, call it CTimer say, then you can make an instance somewhere global and just call .StartCounter and .TimeElapsed
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
-
2I implemented this in Excel VBA (adding in the Overhead as mentioned in this KB article: support.microsoft.com/kb/172338. It worked great. Thanks. – Lance Roberts Oct 14 '08 at 22:55
-
1Thanks, this works well for me, too.
TimeElapsed()gives the result in milliseconds. I did not implement any overhead compensation because I was more worried about the effect of a stutter in the overhead calculation than perfect accuracy. – Justin Jan 11 '11 at 15:59 -
1That's a lot of overheard (in lines of code to manage) -- if you can live with ~10ms accuracy, @Kodak's answer below gives the same thing in one line of code (importing
GetTickCountfrom kernel32). – BrainSlugs83 Mar 8 '14 at 0:45 -
How do you use
StartCounterAndTimeElapsed? I did an instance Timer ofCTimerat the beginning andWith StartCounterI just wrote.StartCounterafter my sub began and.TimeElapsedand it answered meInvalid use of property. When I let.StartCounteralone it tells me an object is not set. – Marine1 Apr 27 '17 at 20:25
The Timer function in VBA gives you the number of seconds elapsed since midnight, to 1/100 of a second.
Dim t as single
t = Timer
'code
MsgBox Timer - t
If you need greater resolution, I would simply run the function 1,000 times and divide the total time by 1,000.
-
15That wouldn't work -- you can't get more resolution out of taking the average like that. – Andrew Scagnelli Jun 4 '09 at 13:58
-
2Still, if you're measuring performance in VBA, getting 1/100th of a second resolution is not bad. -- Invoking the timing calls alone could take a couple of ms. If the call is so fast that you need that much resolution to time it, you probably don't need performance data about that call. – BrainSlugs83 Mar 8 '14 at 0:53
-
10
-
1
-
notes: on Mac the Timer is only accurate to one second - and this might get negative numbers if it starts before midnight and ends after midnight – Martin Trummer Aug 4 '16 at 14:39
If you are trying to return the time like a stopwatch you could use the following API which returns the time in milliseconds since system startup:
Public Declare Function GetTickCount Lib "kernel32.dll" () As Long
Sub testTimer()
Dim t As Long
t = GetTickCount
For i = 1 To 1000000
a = a + 1
Next
MsgBox GetTickCount - t, , "Milliseconds"
End Sub
after http://www.pcreview.co.uk/forums/grab-time-milliseconds-included-vba-t994765.html (as timeGetTime in winmm.dll was not working for me and QueryPerformanceCounter was too complicated for the task needed)
-
This is a great answer. Of note: the precision of the returned data is in milliseconds, however, the counter is only accurate to about 1/100th of a second (that is, it could be off by 10 to 16 ms) via MSDN: msdn.microsoft.com/en-us/library/windows/desktop/… – BrainSlugs83 Mar 8 '14 at 0:50
-
hmm, if the resolution is the same here as with the Timer then I would go with the Timer – Kodak Mar 12 '16 at 14:46
-
What is the
Public Declare Function ...part ? It creates an error when adding your code at the bottom of mine – Marine1 Apr 27 '17 at 17:54 -
For newbees, these links explains how to do an automatic profiling of all the subs that you want to time monitor :
http://www.nullskull.com/a/1602/profiling-and-optimizing-vba.aspx
http://sites.mcpher.com/share/Home/excelquirks/optimizationlink see procProfiler.zip in http://sites.mcpher.com/share/Home/excelquirks/downlable-items
We've used a solution based on timeGetTime in winmm.dll for millisecond accuracy for many years. See http://www.aboutvb.de/kom/artikel/komstopwatch.htm
The article is in German, but the code in the download (a VBA class wrapping the dll function call) is simple enough to use and understand without being able to read the article.