STMS() : Better alternative to A_TickCount

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

STMS() : Better alternative to A_TickCount

20 Apr 2014, 16:58

STMS() returns milliseconds elapsed since 16010101000000 UT, and accurate to ± 1ms.

Code: Select all

STMS() { ; System Time in MS / STMS() returns milliseconds elapsed since 16010101000000 UT
Static GetSystemTimeAsFileTime, T1601                              ; By SKAN / 21-Apr-2014  
  If ! GetSystemTimeAsFileTime
       GetSystemTimeAsFileTime := DllCall( "GetProcAddress", UInt
                                , DllCall( "GetModuleHandle", Str,"Kernel32.dll" )
                                , A_IsUnicode ? "AStr" : "Str","GetSystemTimeAsFileTime" ) 
  DllCall( GetSystemTimeAsFileTime, Int64P,T1601  )
Return T1601 // 10000
} ; http://ahkscript.org/boards/viewtopic.php?p=17076#p17076
Comparison: "Actual elapsed time" vs "Query performance counter" vs "STMS" vs "A_TickCount"

Values in milliseconds
ET     QPC    STMS     ATC
 1       1       1       0
 2       2       2       0
 3       3       3       0
 4       4       4       0
 5       5       5       0
 6       6       6      15
 7       7       7       0
 8       8       8      16
 9       9       9       0
10      10      10      16
11      11      11      15
12      12      12      16
13      13      13       0
14      14      14      15
15      15      15      16
16      16      16      16
17      17      17      15
18      18      18      16
19      19      19      16
20      20      20      16
21      21      21      15
22      22      22      31
23      23      23      16
24      24      24      31
25      25      25      16
26      26      26      31
27      27      27      31
28      28      28      31
29      29      29      16
30      30      30      31
31      31      31      31
32      32      32      32
33      33      33      46
34      34      34      32
35      35      35      31
36      36      36      31
37      37      37      47
38      38      38      31
39      39      39      47
40      40      41      31
41      41      41      47
42      42      42      47
43      43      43      31
44      44      44      47
45      45      45      46
46      46      46      47
47      47      47      47
48      48      48      47
49      49      49      47
50      50      50      46
51      51      51      63
52      52      52      47
53      53      53      46
54      54      55      63
55      55      55      47
56      56      56      62
57      57      57      62
58      58      58      47
59      59      59      63
60      60      60      62
61      61      62      62
62      62      62      63
63      63      63      62
64      64      64      63
65      65      65      62
66      66      66      62
67      67      67      78
68      68      68      63
69      69      69      78
70      70      70      62
71      71      71      78
72      72      72      63
73      73      73      78
74      74      74      78
75      75      75      62
76      76      76      78
77      77      77      78
78      78      78      78
79      79      79      78
80      80      80      78
81      81      81      94
82      82      83      78
83      83      83      78
84      84      84      93
85      85      85      78
86      86      86      94
87      87      87      78
88      88      88      93
89      89      89      94
90      90      90      78
91      91      91      94
92      92      92      93
93      93      93      94
94      94      94      93
95      95      95      94
96      96      96      94
97      97      97      93
98      98      98     109
99      99      99      94
In above test, elapsed time were simulated to be perfect increments of ms.
In real world, elapsed time would be fractional and STMS() will err far more than above result, but up to 1 ms only.
My Scripts and Functions: V1  V2
Guest10
Posts: 578
Joined: 01 Oct 2013, 02:50

Re: STMS() : Better alternative to A_TickCount

20 Apr 2014, 17:20

thanks, any examples using this function? :geek:
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

Re: STMS() : Better alternative to A_TickCount

20 Apr 2014, 23:59

so why use STMS instead of QPC ? are there performance benefits?

User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: STMS() : Better alternative to A_TickCount

21 Apr 2014, 06:08

guest3456 wrote:why use STMS instead of QPC ?
I would not suggest QPC for rapid repeated calls. QPC is best used to determine the performance of the script itself.
For any other timing purposes, A_TickCount to should be sufficient and has accuracy of 0-15ms depending on the computer and has far less overhead than QPC.
15ms difference would not mean much when the timing period is like 3000ms ( 15ms is only 0.5% of 3000ms ),
but is considerably higher when timing for 300ms ( 15ms is 5% of 300ms ).
So, when timing period is small and is the determing factor of the result, then STMS() will be better alternative to A_TickCount.
Guest10 wrote:any examples
I have a RAS dial-up monitor and to calculate the peak speed, I run a 250ms timer as follows:

Code: Select all

SetTimer, Timer, 250

...

Timer:

 ET := < find elapsed time >
 Bytes := < find received bytes >

 Bps  := Bps( Bytes, ET ) 
 KBps := Bps / 1024   

Return
Bytes per second ( Bps ) will be only as accurate as elapsed time ( ET ) is.

Code: Select all

Bytes := 4096

ET  := 250 ; presume Timer was exact
STM := 251 ; STMS() difference could be +/- 1ms
ATC := 258 ; A_TickCount difference could be > +/- 10ms

Msgbox %  Bps( Bytes, ET  ) ; 16384 
  .  "`n" Bps( Bytes, STM ) ; 16318  0.4% lesser
  .  "`n" Bps( Bytes, ATC ) ; 15875  3.2% lesser

Bps( Bytes, MS ) {
 Return ( Bytes/1000 ) / ( MS/1000 ) * 1000
}
My Scripts and Functions: V1  V2
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: STMS() : Better alternative to A_TickCount

04 Aug 2014, 05:11

another one

Code: Select all

Clock()
{
    return DllCall("msvcrt.dll\clock")
}
ZwDelayExecution(Alertable, Interval)
{
    DllCall("Ntdll.dll\ZwDelayExecution", "UChar", Alertable, "Int64*", Interval)
}

start := Clock()
ZwDelayExecution(0, -30000000)                 ; 3 sec
finish := Clock()
MsgBox % (finish - start) / 1000 " sec"        ; ==> 3.010000 sec
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: Gianna999 and 151 guests