Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if_vs_ternary_vs_switch()
- if_vs_ternary_vs_switch()
- {
- ; Testing speed of if vs ternary vs switch
- ; Iterations: 100,000,000 of 4 loops
- ; Results:
- ; Tern: 77.010257
- ; Switch: 95.079987
- ; If/else: 110.643269
- iterations := 10000000
- str := ""
- qpx(1)
- t := 0
- f := 0
- Loop, % iterations
- {
- Loop, 4
- If (A_Index = 3)
- t++
- Else If (A_Index = 2)
- t++
- Else If (A_Index = 1)
- t++
- else f++
- }
- str .= "`nIf/else: " qpx()
- qpx(1)
- t := 0
- f := 0
- Loop, % iterations
- {
- Loop, 4
- (A_Index = 3) ? t++
- : (A_Index = 2) ? t++
- : (A_Index = 1) ? t++
- : f++
- }
- str .= "`nTern: " qpx()
- qpx(1)
- t := 0
- f := 0
- Loop, % iterations
- {
- Loop, 4
- Switch A_Index
- {
- Case 3: t++
- Case 2: t++
- Case 1: t++
- Default: f++
- }
- }
- str .= "`nSwitch: " qpx()
- MsgBox, % str
- Return
- }
- ; qpx(1) starts it and qpx() stops timer and returns time
- qpx(N=0) { ; Wrapper for QueryPerformanceCounter() by SKAN | CD: 06/Dec/2009
- Local ; www.autohotkey.com/forum/viewtopic.php?t=52083 | LM: 10/Dec/2009
- Static F:="", A:="", Q:="", P:="", X:=""
- If (N && !P)
- Return DllCall("QueryPerformanceFrequency",Int64P,F) + (X:=A:=0)
- + DllCall("QueryPerformanceCounter",Int64P,P)
- DllCall("QueryPerformanceCounter",Int64P,Q), A:=A+Q-P, P:=Q, X:=X+1
- Return (N && X=N) ? (X:=X-1)<<64 : (N=0 && (R:=A/X/F)) ? (R + (A:=P:=X:=0)) : 1
- }
Advertisement
Add Comment
Please, Sign In to add comment