Is switch/case run faster than if ? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
magicshow
Posts: 67
Joined: 14 Oct 2022, 11:38

Is switch/case run faster than if ?

Post by magicshow » 01 Feb 2023, 02:01

Code: Select all

if (M_layout =  "hold")
{
	Guicontrol ,Xmenu_M_Menu:show,M_label_1
	Guicontrol ,Xmenu_M_Menu:show,M_label_2
	Guicontrol ,Xmenu_M_Menu:show,M_label_3
	Guicontrol ,Xmenu_M_Menu:show,M_label_4
	Guicontrol ,Xmenu_M_Menu:hide,M_label_5
	Guicontrol ,Xmenu_M_Menu:show,M_label_q
	Guicontrol ,Xmenu_M_Menu:show,M_label_e
	Guicontrol ,Xmenu_M_Menu:show,M_label_a
	Guicontrol ,Xmenu_M_Menu:show,M_label_b

}

if (M_layout =  "testing")
{
	Guicontrol ,Xmenu_M_Menu:hide,M_label_1
	Guicontrol ,Xmenu_M_Menu:hide,M_label_2
	Guicontrol ,Xmenu_M_Menu:hide,M_label_3
	Guicontrol ,Xmenu_M_Menu:hide,M_label_4
	Guicontrol ,Xmenu_M_Menu:show,M_label_5
	Guicontrol ,Xmenu_M_Menu:show,M_label_q
	Guicontrol ,Xmenu_M_Menu:show,M_label_e
	Guicontrol ,Xmenu_M_Menu:show,M_label_a
	Guicontrol ,Xmenu_M_Menu:show,M_label_b
}
I got 7 blocks (and more to come) simple if like this, will the script run faster when i put M_layout to a switch/case ? If no i will leave it this way...

User avatar
boiler
Posts: 16960
Joined: 21 Dec 2014, 02:44

Re: Is switch/case run faster than if ?  Topic is solved

Post by boiler » 01 Feb 2023, 03:27

If one way is faster than the other, it would be indiscernible.

Rohwedder
Posts: 7647
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Is switch/case run faster than if ?

Post by Rohwedder » 01 Feb 2023, 05:11

Hallo,
this usually takes 6 µs here:

Code: Select all

DllCall("QueryPerformanceFrequency",Int:="Int64*",Freq),Time:=Count:=0
q::
;Multiple execution increases accuracy by means of averaging!
M_layout := "Exit9"
DllCall("QueryPerformanceCounter",Int,Count1)
IF (M_layout = "Exit1")
	M_layout = 1
IF (M_layout = "Exit2")
	M_layout = 2
IF (M_layout = "Exit3")
	M_layout = 3
IF (M_layout = "Exit4")
	M_layout = 4
IF (M_layout = "Exit5")
	M_layout = 5
IF (M_layout = "Exit6")
	M_layout = 6
IF (M_layout = "Exit7")
	M_layout = 7
IF (M_layout = "Exit8")
	M_layout = 8
IF (M_layout = "Exit9")
	M_layout = 9		
DllCall("QueryPerformanceCounter",Int,Count2)
ToolTip,% "Time: " Round((Time+=(Count2-Count1)/Freq*1.E6)/++Count) " µs"
Return
while this is mostly faster with 5 µs:

Code: Select all

DllCall("QueryPerformanceFrequency",Int:="Int64*",Freq),Time:=Count:=0
q::
;Multiple execution increases accuracy by means of averaging!
M_layout := "Exit9"
DllCall("QueryPerformanceCounter",Int,Count1)
Switch M_layout
{
Case "Exit1":
	M_layout = 1
Case "Exit2":
	M_layout = 2
Case "Exit3":
	M_layout = 3
Case "Exit4":
	M_layout = 4
Case "Exit5":
	M_layout = 5
Case "Exit6":
	M_layout = 6
Case "Exit7":
	M_layout = 7
Case "Exit8":
	M_layout = 8
Case "Exit9":
	M_layout = 9
}	
DllCall("QueryPerformanceCounter",Int,Count2)
ToolTip,% "Time: " Round((Time+=(Count2-Count1)/Freq*1.E6)/++Count) " µs"
Return
i agree with boiler.

Post Reply

Return to “Ask for Help (v1)”