06-Oct-2020 02:51
I saw the article in https://devcentral.f5.com/s/articles/irules-101-04-switch saying Switch is a higher performing command and run faster than If command.
Why is that so?
Solved! Go to Solution.
06-Oct-2020
10:29
- last edited on
04-Jun-2023
21:16
by
JimmyPackets
Yes Morris. That's right.
If-statement with 3 conditions (and default).
if { $bla eq "one" } {
# do something
} elseif { $bla eq "two" } {
# do something
} elseif { $bla eq "three" } {
# do something
} else {
# do something
}
Switch-statement with 3 conditions (and default).
switch $bla {
"one" {
# do something
}
"two" {
# do something
}
"three" {
# do something
}
default {
# do something
}
}
Don't forget to mark my answer as the best the help me for the contribution.
Regards,
Dario.
06-Oct-2020 04:28
Hello Morris.
The same article states this:
"Generally, switch commands are faster than if statements due to additional expression evaluations that need to occur with if commands. Since the switch statements only works on a single comparison value, internal optimizations are able to be made in the evaluation process. This likely isn't going to be a big difference for one or two comparisons, but it is measurable when more are made."
It's because how the if statement is implemented at machine level.
Some reference:
http://www.blackwasp.co.uk/speedtestifelseswitch.aspx
Regards,
Dario.
06-Oct-2020 08:22
hmm, quite interesting article.
So, basically a switch statement with 1 or 2 conditions won't make much difference than if-else statement but more than 5 "switch statement" would be a better choice. Am I understand correctly?
Any example code for a switch statement with 5 conditions?
06-Oct-2020
10:29
- last edited on
04-Jun-2023
21:16
by
JimmyPackets
Yes Morris. That's right.
If-statement with 3 conditions (and default).
if { $bla eq "one" } {
# do something
} elseif { $bla eq "two" } {
# do something
} elseif { $bla eq "three" } {
# do something
} else {
# do something
}
Switch-statement with 3 conditions (and default).
switch $bla {
"one" {
# do something
}
"two" {
# do something
}
"three" {
# do something
}
default {
# do something
}
}
Don't forget to mark my answer as the best the help me for the contribution.
Regards,
Dario.
06-Oct-2020 20:05
Thanks!