-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathstopwatch.ahk
69 lines (52 loc) · 1.81 KB
/
stopwatch.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
; returns elapsed time in ms - very accurate
; Can keep track of multiple events
; doesn't use any resources (cpu time) between function calls
; Start a timer:
; timerID := stopwatch() ; call function with no parameters to get a timer ID
;
; Retrieve elapsed time:
; time := stopwatch(timerID, mode)
; mode = 1 ; remove timer
; mode = -1 ; reset timer to 0
; mode = 0 ; do not alter timer (leave it 'running' and just get the the time elapsed)
stopwatch(itemId := 0, mode := 1)
{
; static F := DllCall("QueryPerformanceFrequency", "Int64P", F) * F , aTicks := [], runID := 0
static F := DllCall("QueryPerformanceFrequency", "Int64P", F) * F , runID := 0
if (itemId = 0) ; = 0 so if user accidentally passes an empty or invalid ID-variable function returns -1
return ++runID, DllCall("QueryPerformanceCounter", "Int64P", S), aTicks[runID] := S
if aTicks.hasKey(itemId)
{
DllCall("QueryPerformanceCounter", "Int64P", now)
timeElapsed := (now - aTicks[itemId]) / F * 1000
if (mode > 0)
aTicks.remove(itemId, "")
else if (mode < 0)
DllCall("QueryPerformanceCounter", "Int64P", S), aTicks[itemId] := S
return timeElapsed
}
else ; a blank variable or non-existent ID was passed
return -1
}
/*
; returns elapsed time in ms
; Can keep track of multiple events
stopwatch(itemId := 0, removeUsedItem := True)
{
static F := DllCall("QueryPerformanceFrequency", "Int64P", F) * F , aTicks := [], runID := 0
if (itemId = 0) ; so if user accidentally passes an empty ID variable function returns -1
{
DllCall("QueryPerformanceCounter", "Int64P", S), aTicks[++runID] := S
return runID
}
else
{
if aTicks.hasKey(itemId)
{
DllCall("QueryPerformanceCounter", "Int64P", End)
return (End - aTicks[itemId]) / F * 1000, removeUsedItem ? aTicks.remove(itemId, "") : ""
}
else return -1
}
}
*/