Forum Discussion
string equal/compare/match vs. switch
All,
Very new to iRules. Wondering if anyone knows what is more efficient:
switch $foo {
"bar" {...}
"bar1" {...}
"bar2" {...}
default {...}
}
vs.
if {string equal $foo "bar"} {...}
elseif {string equal $foo "bar1" {...}
elseif {string equal $foo "bar2" {...}
else {...}
vs.
if {string compare $foo "bar"} {...}
elseif {string compare $foo "bar1" {...}
elseif {string compare $foo "bar2" {...}
else {...}
vs.
if {string match $foo "bar"} {...} Seems like this would not be the most optimal
elseif {string match $foo "bar1" {...}
elseif {string match $foo "bar2" {...}
else {...}
or simply,
if {$foo equals "bar"} {...}
elseif {$foo equals "bar1" {...}
elseif {$foo equals "bar2" {...}
else {...}
Cheers!
3 Replies
- IheartF5_45022
Nacreous
switch is more efficient for more than 4 or 5 elseif I believe.
I tend to use switch for anymore than 1 as I like the format better, even though it's slightly less efficient. This might be useful https://devcentral.f5.com/articles/irules-optimization-101-01-if-elseif-and-switch.UxAwSEq4aM8
- Kevin_Stewart
Employee
Here's a fun way to test the efficiency of each command. Create a text file in the shell:
!/usr/bin/tclsh proc random { } { set mylist [list "bar" "bar1" "bar2" "blah"] set index [expr int(rand() * 4)] return [lindex $mylist $index] } set foo [random] set timer [time { switch $foo { "bar" { set test "bar" } "bar1" { set test "bar1" } "bar2" { set test "bar2" } default {set test "def" } } } 1000000] puts "switch: $timer" set timer [time { if { [string equal $foo "bar"] } { set test "bar" } elseif { [string equal $foo "bar1"] } { set test "bar1" } elseif { [string equal $foo "bar2"] } { set test "bar2" } else { set test "def" } } 1000000] puts "str equal: $timer" set timer [time { if { [string compare $foo "bar"] } { set test "bar" } elseif { [string compare $foo "bar1"] } { set test "bar1" } elseif { [string compare $foo "bar2"] } { set test "bar2" } else { set test "def" } } 1000000] puts "compare: $timer" set time [time { if { [string match $foo "bar"] } { set test "bar" } elseif { [string match $foo "bar1"] } { set test "bar1" } elseif { [string match $foo "bar2"] } { set test "bar2" } else { set test "def" } } 1000000] puts "match: $timer" set timer [time { if { $foo == "bar" } { set test "bar" } elseif { $foo == "bar1" } { set test "bar1" } elseif { $foo == "bar2" } { set test "bar2" } else { set test "def" } } 1000000] puts "equals: $timer"Then chmod the file and run in it. It may be an anomaly, but I almost consistently get lower times for match and compare, followed closely by switch.
- Heisenberg_1452Historic F5 Account
Chaps, thank you for the response. Very helpful! Cheers!
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com