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:

  1. Method 3 - 00:00:01 - 009 ms - 000 µs - 000 ns
  2. Method 3 - 00:00:01 - 008 ms - 000 µs - 000 ns
  3. Method 3 - 00:00:01 - 004 ms - 000 µs - 000 ns
  4. Method 3 - 00:00:01 - 003 ms - 000 µs - 000 ns
  5. Method 3 - 00:00:01 - 015 ms - 000 µs - 000 ns
  6. Method 3 - 00:00:01 - 003 ms - 000 µs - 000 ns
  7. Method 3 - 00:00:01 - 007 ms - 000 µs - 000 ns
  8. Method 3 - 00:00:01 - 004 ms - 000 µs - 000 ns
  9. Method 3 - 00:00:01 - 016 ms - 000 µs - 000 ns
  10. Method 3 - 00:00:01 - 005 ms - 000 µs - 000 ns
  11. Method 3 - 00:00:01 - 012 ms - 000 µs - 000 ns
  12. Method 3 - 00:00:01 - 001 ms - 000 µs - 000 ns
  13. Method 3 - 00:00:01 - 007 ms - 000 µs - 000 ns
  14. Method 3 - 00:00:01 - 003 ms - 000 µs - 000 ns
  15. Method 3 - 00:00:01 - 003 ms - 000 µs - 000 ns
  16. Method 3 - 00:00:01 - 013 ms - 000 µs - 000 ns
  17. Method 3 - 00:00:01 - 009 ms - 000 µs - 000 ns
  18. Method 3 - 00:00:01 - 001 ms - 000 µs - 000 ns
  19. Method 3 - 00:00:01 - 015 ms - 000 µs - 000 ns
  20. 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.

  1. Method 3 - 00:00:01 - 000 ms - 000 µs - 000 ns
  2. Method 3 - 00:00:01 - 001 ms - 000 µs - 000 ns
  3. Method 3 - 00:00:01 - 000 ms - 000 µs - 000 ns
  4. Method 3 - 00:00:01 - 001 ms - 000 µs - 000 ns
  5. Method 3 - 00:00:01 - 000 ms - 000 µs - 000 ns
  6. Method 3 - 00:00:01 - 000 ms - 000 µs - 000 ns
  7. Method 3 - 00:00:01 - 001 ms - 000 µs - 000 ns
  8. Method 3 - 00:00:01 - 001 ms - 000 µs - 000 ns
  9. Method 3 - 00:00:01 - 000 ms - 000 µs - 000 ns
  10. Method 3 - 00:00:01 - 001 ms - 000 µs - 000 ns
  11. Method 3 - 00:00:01 - 000 ms - 000 µs - 000 ns
  12. Method 3 - 00:00:01 - 000 ms - 000 µs - 000 ns
  13. Method 3 - 00:00:01 - 001 ms - 000 µs - 000 ns
  14. Method 3 - 00:00:01 - 000 ms - 000 µs - 000 ns
  15. Method 3 - 00:00:01 - 001 ms - 000 µs - 000 ns
  16. Method 3 - 00:00:01 - 001 ms - 000 µs - 000 ns
  17. Method 3 - 00:00:01 - 001 ms - 000 µs - 000 ns
  18. Method 3 - 00:00:01 - 001 ms - 000 µs - 000 ns
  19. Method 3 - 00:00:01 - 001 ms - 000 µs - 000 ns
  20. 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)