TimeGetTime (Multimedia Timer) - danielep71/VBA-PERFORMANCE GitHub Wiki
[Home]] ](/danielep71/VBA-PERFORMANCE/wiki/[[Class-cPerformanceMonitor)
Another possible approach would be to employ the timeGetTime function.
The timeGetTime function is based on winmm.dll (winmm.dll is a library for the Windows Multimedia API. The file is loaded into the RAM and runs there as a Windows process, also called a “task”).
Like GetTickCount, it returns the number of milliseconds that have elapsed since Windows was started
TimeGetTime is supposed to have a better resolution than the GetTickCount API. Both have the same wrap time, and the same return value.
Still, the resolution of timeGetTime is adjustable with the timeBeginPeriod / timeEndPeriod functions.
By default, GetTickCount and timeGetTime have the same resolution: 15.625ms, but after calling timeBeginPeriod(1), GetTickCount still updates every 15.625 ms, while timeGetTime does update every 1ms.
The timeGetTime uses the winmm.dll, which has more overhead than using the counterpart, GetTickCount which uses the kernel32.dll file.
1. API Declaration
#If VBA7 Then
Private Declare PtrSafe Function timeGetTime Lib "winmm.dll" () As _
LongPtr
Private Declare PtrSafe Function timeBeginPeriod Lib "winmm.dll" _
(ByVal uPeriod As Integer) As Integer
Private Declare PtrSafe Function timeEndPeriod Lib "winmm.dll" _
(ByVal uPeriod As Integer) As Integer
#Else
Private Declare Function timeGetTime Lib "winmm.dll" Alias _
"timeGetTime" () As Long
Private Declare Function timeBeginPeriod Lib "winmm.dll" (ByVal _
uPeriod As Integer) As Integer
Private Declare Function timeEndPeriod Lib "winmm.dll" (ByVal _
uPeriod As Integer) As Integer
#End If
This declaration is valid regardless of Excel bitness and VBA version.
3.2. Rollover
The return value wraps around to 0 every 2^32 milliseconds, about 49.71 days.
3.3. Accuracy and resolution
Following are the test results:
- Method 3 - 00:00:01 - 009 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 008 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 004 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 003 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 015 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 003 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 007 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 004 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 016 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 005 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 012 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 001 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 007 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 003 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 003 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 013 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 009 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 001 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 015 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 003 ms - 000 µs - 000 ns
Accuracy is up to 15/16 milliseconds.
Using the NextTick Function, the results are much better.
- Method 3 - 00:00:01 - 000 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 001 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 000 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 001 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 000 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 000 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 001 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 001 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 000 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 001 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 000 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 000 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 001 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 000 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 001 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 001 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 001 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 001 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 001 ms - 000 µs - 000 ns
- Method 3 - 00:00:01 - 000 ms - 000 µs - 000 ns
Accuracy is now up to 1 millisecond.
The resolution is in milliseconds, the minimum available using the API Function timeBeginPeriod 1.
[Home]] ](/danielep71/VBA-PERFORMANCE/wiki/[[Class-cPerformanceMonitor)