Disagree with your test result.
Hi Kedaman, I was disagree with you test result, because i did the same Timer Function omparison as well and I found that the MultiMedia Timer API function really can go up to 1ms accuratecy.
Although the QueryPerformanceCounter and QueryPerformanceFrequency can go up to 1ms precision as well, but the refresh interval just a bit longer than the Multimedis Timer API.
Below just a summerize from the result that I get.
- SetTimer API - Accuratecy up to 10ms
- GetTickCount API - Accuratecy up to 10ms
- QueryPerformanceCounter API - Accuratecy up to 1ms
- MultiMedia Timer API - Accuratecy up to 1ms
- Timer Function - Accuratecy up to 1s
So, it is better to use the MultiMedia Timer API function. If and only if your program really need that precision. Else you can go for the QueryPerformanceCounter or GetTickCount API function.
Below is those API function that I used in my test procedure:
[code]
'SetTimer API
Private Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Private Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
'GetTickCount Timer
Private Declare Function GetTickCount Lib "kernel32" () As Long
'QueryPerformaceCounter Timer
Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As Currency) As Long
Private Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As Currency) As Long
'MultiMedia Timer
Public Declare Function timeKillEvent Lib "winmm.dll" (ByVal uId As Long) As Long
Private Declare Function timeSetEvent Lib "winmm.dll" (ByVal uDelay As Long, ByVal uResolution As Long, ByVal lpFunction As Long, ByVal dwUser As Long, ByVal uFlags As Long) As Long
Private Const TIME_ONESHOT = 0 ' program timer for single event
Private Const TIME_PERIODIC = 1 ' program for continuous periodic event
Private Const TIME_CALLBACK_FUNCTION = 0
Here is part of my test result for using various timer function and API to count for 3 second.
Code:
SetTimer GetTickCount QueryPerformance MultiMedia Timer Timer Function
------------------------------------------------------------------------------
11071159 11067824 11065 11061875 39788
11071169 11067824 11065 11061875 39788
11071179 11067824 11065 11061875 39788
11071189 11067824 11065 11061875 39788
11071199 11067824 11065 11061886 39788
11071209 11067824 11065 11061886 39788
11071219 11067824 11065 11061886 39788
11071229 11067824 11065 11061887 39788
11071239 11067824 11065 11061887 39788
11071249 11067824 11065 11061887 39788
11071259 11067824 11065 11061887 39788
11071269 11067824 11065 11061888 39788
11071279 11067824 11065 11061888 39788
11071289 11067824 11065 11061888 39788
11071299 11067824 11065 11061888 39788
11071309 11067824 11065 11061889 39788
11071319 11067824 11065 11061889 39788
11071329 11067824 11065 11061890 39788
11071339 11067824 11065 11061890 39788
11071349 11067824 11065 11061890 39788
11071359 11067824 11065 11061890 39788
11071369 11067824 11065 11061891 39788
11071379 11067824 11065 11061891 39788
11071389 11067824 11065 11061892 39788
11071399 11067834 11065 11061892 39788
11071409 11067834 11065 11061893 39788
11071419 11067834 11065 11061893 39788
11071429 11067834 11065 11061893 39788
11071439 11067834 11065 11061894 39788
11071449 11067834 11065 11061895 39788
11071459 11067834 11065 11061896 39788
11071469 11067834 11065 11061897 39788
11071479 11067834 11065 11061898 39788
11071489 11067834 11065 11061899 39788
11071499 11067834 11065 11061900 39788
11071509 11067834 11065 11061901 39788
11071520 11067834 11065 11061902 39788
11071530 11067834 11065 11061903 39788
11071540 11067834 11065 11061904 39788
11071550 11067854 11065 11061905 39788
11071560 11067854 11065 11061906 39788
11071570 11067854 11065 11061907 39788
11071580 11067854 11065 11061908 39788
11071590 11067864 11065 11061909 39788
11071600 11067864 11065 11061910 39788
11071610 11067864 11065 11061911 39788
11071620 11067864 11065 11061912 39788
11071630 11067864 11065 11061913 39788
11071640 11067864 11065 11061914 39788
11071650 11067864 11065 11061915 39788
11071660 11067864 11065 11061916 39788
11071670 11067864 11065 11061917 39788
11071680 11067864 11065 11061918 39788
11071690 11067864 11065 11061919 39788
11071700 11067864 11065 11061920 39788
11071710 11067864 11065 11061921 39788
11071720 11067864 11065 11061922 39788
11071730 11067864 11065 11061923 39788
11071740 11067874 11065 11061924 39788
11071750 11067874 11065 11061925 39788
11071760 11067874 11065 11061926 39788
11071770 11067874 11065 11061927 39788
11071780 11067874 11065 11061928 39788
11071790 11067874 11065 11061929 39788
11071800 11067874 11065 11061930 39788
11071810 11067874 11065 11061931 39788
11071820 11067874 11065 11061932 39788
11071830 11067874 11065 11061933 39788
11071840 11067874 11065 11061934 39788
11071850 11067874 11065 11061935 39788
11071860 11067874 11065 11061946 39788
11071870 11067884 11065 11061947 39788
11071880 11067884 11065 11061948 39788
11071890 11067884 11065 11061948 39788
11071900 11067884 11065 11061949 39788
11071910 11067884 11065 11061949 39788
11071920 11067884 11065 11061950 39788
11071930 11067884 11065 11061951 39788
11071940 11067884 11065 11061951 39788
11071950 11067884 11065 11061952 39788
11071960 11067904 11065 11061953 39788
11071970 11067904 11065 11061953 39788
11071980 11067904 11065 11061954 39788
11071990 11067904 11065 11061954 39788
11072000 11067904 11065 11061955 39788
11072010 11067904 11065 11061956 39788
11072020 11067914 11065 11061957 39788
11072030 11067914 11065 11061957 39788
11072040 11067914 11065 11061958 39788
11072050 11067914 11065 11061958 39788
11072060 11067914 11065 11061959 39788
11072070 11067914 11065 11061960 39788
11072080 11067914 11065 11061961 39788
11072090 11067914 11065 11061961 39788
11072100 11067914 11065 11061962 39788
11072110 11067914 11065 11061962 39788
11072120 11067914 11065 11061963 39788
11072130 11067914 11065 11061964 39788
11072140 11067924 11065 11061965 39788
11072150 11067924 11065 11061965 39788
11072160 11067924 11065 11061966 39788
11072170 11067924 11065 11061967 39788
11072180 11067924 11065 11061968 39788
11072190 11067924 11065 11061969 39788
11072200 11067924 11065 11061970 39788
11072210 11067924 11065 11061971 39788
11072221 11067924 11065 11061972 39788
11072231 11067924 11065 11061973 39788
11072241 11067924 11065 11061974 39788
11072251 11067924 11065 11061975 39788
11072261 11067934 11065 11061976 39788
11072271 11067934 11065 11061977 39788
11072281 11067934 11065 11061978 39788
11072291 11067934 11065 11061979 39788
11072301 11067934 11065 11061980 39788
11072311 11067934 11065 11061981 39788
11072321 11067934 11065 11061982 39788
11072331 11067934 11065 11061983 39788
11072341 11067934 11065 11061984 39788
11072351 11067934 11065 11061985 39788
11072361 11067934 11065 11061986 39788
11072371 11067954 11065 11061990 39788
11072381 11067954 11065 11061991 39788
11072391 11067954 11065 11061991 39788
11072401 11067954 11065 11061992 39788
11072411 11067954 11065 11061993 39788
11072421 11067954 11065 11061994 39788
11072431 11067954 11065 11061995 39788
11072441 11067964 11065 11061995 39788
11072451 11067964 11065 11061996 39788
11072461 11067964 11065 11061997 39788
11072471 11067964 11065 11061998 39788
11072481 11067964 11065 11061999 39788
11072491 11067964 11065 11062000 39788
11072501 11067964 11065 11062000 39788
11072511 11067964 11065 11062001 39788
11072521 11067964 11065 11062002 39788
11072531 11067964 11065 11062003 39788
11072541 11067964 11065 11062004 39788
11072551 11067964 11065 11062005 39789
11072561 11067974 11065 11062006 39789
11072571 11067974 11065 11062007 39789
11072581 11067974 11065 11062008 39789
11072591 11067974 11065 11062009 39789
11072601 11067974 11065 11062010 39789
11072611 11067974 11065 11062011 39789
11072621 11067974 11065 11062013 39789
11072631 11067974 11065 11062013 39789
11072641 11067974 11065 11062014 39789
11072651 11067974 11065 11062015 39789
11072661 11067984 11065 11062016 39789
11072671 11067984 11065 11062017 39789
11072681 11067984 11065 11062018 39789
11072691 11067984 11065 11062019 39789
11072701 11067984 11065 11062020 39789
11072711 11067984 11065 11062021 39789
11072721 11067984 11065 11062022 39789
11072731 11067984 11065 11062023 39789
11072741 11067984 11065 11062024 39789
11072751 11067984 11065 11062025 39789
11072761 11067994 11065 11062026 39789
11072771 11067994 11065 11062027 39789
11072781 11067994 11065 11062028 39789
11072791 11068004 11065 11062029 39789
11072801 11068004 11065 11062030 39789
11072811 11068004 11065 11062031 39789
11072821 11068004 11065 11062032 39789
11072831 11068004 11065 11062033 39789
11072841 11068004 11065 11062034 39789
11072851 11068004 11065 11062035 39789
11072861 11068004 11065 11062037 39789
11072871 11068004 11065 11062037 39789
11072881 11068004 11065 11062039 39789
11072891 11068014 11065 11062039 39789
11072901 11068014 11065 11062041 39789
11072912 11068014 11065 11062042 39789
11072922 11068014 11065 11062043 39789
11072932 11068014 11065 11062044 39789
11072942 11068014 11065 11062045 39789
11072952 11068014 11065 11062046 39789
11072962 11068014 11065 11062047 39789
11072972 11068014 11065 11062048 39789
11072982 11068024 11065 11062049 39789
11072992 11068024 11065 11062050 39789
11073002 11068024 11065 11062051 39789
11073012 11068024 11065 11062052 39789
11073022 11068024 11065 11062053 39789
11073032 11068024 11065 11062054 39789
11073042 11068024 11065 11062055 39789
11073052 11068024 11065 11062056 39789
11073062 11068024 11065 11062057 39789
11073072 11068034 11065 11062058 39789
11073082 11068034 11065 11062059 39789
11073092 11068034 11065 11062060 39789
11073102 11068034 11065 11062061 39789
11073112 11068034 11065 11062062 39789
11073122 11068034 11065 11062063 39789
11073132 11068034 11065 11062064 39789
11073142 11068034 11065 11062065 39789
11073152 11068034 11065 11062066 39789
11073162 11068034 11066 11062067 39789
11073172 11068045 11066 11062068 39789
11073182 11068045 11066 11062069 39789
11073192 11068045 11066 11062070 39789
11073202 11068045 11066 11062071 39789
11073212 11068045 11066 11062072 39789
11073222 11068045 11066 11062074 39789
11073232 11068055 11066 11062074 39789
11073242 11068055 11066 11062076 39789
11073252 11068055 11066 11062077 39789
11073262 11068055 11066 11062078 39789
11073272 11068055 11066 11062079 39789
11073282 11068055 11066 11062080 39789
11073292 11068055 11066 11062081 39789
11073302 11068055 11066 11062083 39789
11073312 11068055 11066 11062083 39789
11073322 11068065 11066 11062085 39789
11073332 11068065 11066 11062087 39789
11073342 11068065 11066 11062088 39789
11073352 11068065 11066 11062089 39789
11073362 11068065 11066 11062091 39789
11073372 11068065 11066 11062092 39789
11073382 11068065 11066 11062093 39789
11073392 11068065 11066 11062094 39789
11073402 11068075 11066 11062095 39789
11073412 11068075 11066 11062096 39789
11073422 11068075 11066 11062098 39789
11073432 11068075 11066 11062098 39789
11073442 11068075 11066 11062100 39789
11073452 11068075 11066 11062101 39789
11073462 11068075 11066 11062102 39789
11073472 11068075 11066 11062103 39789
11073482 11068075 11066 11062107 39789
11073492 11068085 11066 11062108 39789
11073502 11068085 11066 11062110 39789
11073512 11068085 11066 11062111 39789
11073522 11068085 11066 11062112 39789
11073532 11068085 11066 11062113 39789
11073542 11068085 11066 11062115 39789
11073552 11068085 11066 11062116 39789
11073562 11068085 11066 11062117 39789
11073572 11068095 11066 11062118 39789
11073582 11068095 11066 11062119 39789
11073592 11068095 11066 11062120 39789
11073602 11068095 11066 11062122 39789
11073613 11068095 11066 11062123 39789
11073623 11068095 11066 11062124 39789
11073633 11068095 11066 11062125 39789
11073643 11068105 11066 11062127 39789
11073653 11068105 11066 11062128 39789
11073663 11068105 11066 11062129 39789
11073673 11068105 11066 11062130 39789
11073683 11068105 11066 11062132 39789
11073693 11068105 11066 11062133 39789
11073703 11068105 11066 11062135 39789
11073713 11068105 11066 11062137 39789
11073723 11068115 11066 11062138 39789
11073733 11068115 11066 11062139 39789
11073743 11068115 11066 11062141 39789
11073753 11068115 11066 11062142 39789
11073763 11068115 11066 11062143 39789
11073773 11068125 11066 11062144 39789
11073783 11068125 11066 11062146 39789
11073793 11068125 11066 11062147 39789
11073803 11068125 11066 11062148 39789
11073813 11068125 11066 11062149 39789
11073823 11068125 11066 11062151 39789
11073833 11068125 11066 11062152 39789
11073843 11068135 11066 11062153 39789
11073853 11068135 11066 11062154 39789
11073863 11068135 11066 11062156 39789
11073873 11068135 11066 11062157 39789
11073883 11068135 11066 11062159 39789
11073893 11068135 11066 11062160 39789
11073903 11068135 11066 11062161 39789
11073913 11068135 11066 11062162 39789
11073923 11068145 11066 11062164 39789
11073933 11068145 11066 11062165 39789
11073943 11068145 11066 11062167 39789
11073953 11068145 11066 11062168 39789
11073963 11068145 11066 11062169 39789
11073973 11068145 11066 11062170 39789
11073983 11068155 11066 11062172 39789
11073993 11068155 11066 11062173 39789
11074003 11068155 11066 11062175 39789
11074013 11068155 11066 11062176 39789
11074023 11068155 11066 11062177 39789
11074033 11068155 11066 11062178 39789
11074043 11068155 11066 11062180 39789
11074053 11068155 11066 11062181 39789
11074063 11068165 11066 11062183 39789
11074073 11068165 11066 11062185 39789
11074083 11068165 11066 11062187 39789
11074093 11068165 11066 11062188 39789
11074103 11068165 11066 11062190 39789
11074113 11068165 11066 11062191 39789
11074123 11068175 11066 11062192 39789
11074133 11068175 11066 11062194 39789
11074143 11068175 11066 11062195 39789
11074153 11068175 11066 11062196 39789
QueryPerformanceCounter & QueryPerformanceFrequency API
Hi! Kedaman, I just refer back the book that I've bought (Visual Basic With Win32 API By Jason Bock (Wrox Publisher) and I found that my test program did have some problem in the QueryPerformanceCounter and QueryPerformanceFrequency API function.
According to that book, he said that the QueryperformanceCounter and QueryPerformanceFrequency can have the precision upto nanosecond which you did mention before and you're rite. :)
For my testing, I did each API function seperately instead of all together.
Here is my SetTimer and KillTimer API function call hope I did made any mistake in the test routine. ;)
Code:
Option Explicit
Private T1&, T2&, dl&
Private Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
Private Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
Public Sub load_SetTimer()
SetTimer frmMain.hwnd, 0, 1, AddressOf TimerProc
T1 = GetTickCount
frmMain.cmdAction(0).Enabled = False
End Sub
Sub TimerProc(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
If T2 - T1 < 3000 Then
T2 = GetTickCount
frmMain.txtOutput(0).Text = frmMain.txtOutput(0).Text & T2 & vbCrLf
Else
KillTimer frmMain.hwnd, 0
End If
End Sub
and this the test routine for the timeSetEvents and timeKillEvents
[code]
Public Declare Function timeKillEvent Lib "winmm.dll" (ByVal uId As Long) As Long
Private Declare Function timeSetEvent Lib "winmm.dll" (ByVal uDelay As Long, ByVal uResolution As Long, ByVal lpFunction As Long, ByVal dwUser As Long, ByVal uFlags As Long) As Long
Private Const TIME_PERIODIC = 1 ' program for continuous periodic event
Private Const TIME_CALLBACK_FUNCTION = 0
Public mTimerID&
Public Sub load_MultiMediaTimer()
mTimerID = timeSetEvent(1, 0, AddressOf OnTimer, 0, TIME_CALLBACK_FUNCTION Or TIME_PERIODIC)
T1 = timeGetTime
End Sub
Private Sub OnTimer(ByVal uId As Long, ByVal uMsg As Long, ByVal dwUser As Long, ByVal dw1 As Long, ByVal dw2 As Long)
If T2 - T1 < 3000 Then
T2 = timeGetTime
frmMain.txtOutput(3).Text = frmMain.txtOutput(3).Text & T2 & vbCrLf
Else
timeKillEvent mTimerID
End If
End Sub