Case select versus IF Statement
-
Which is faster? I have a class that loops through consitantly monitoring input and filters down tot he correct path based off multiple factors, I noticed now that my paths that are down the nested if by 7 or 8 layers take as long as 2 seconds to execute. Will porting over my code to select / case statements increase efficiency?
-
Which is faster? I have a class that loops through consitantly monitoring input and filters down tot he correct path based off multiple factors, I noticed now that my paths that are down the nested if by 7 or 8 layers take as long as 2 seconds to execute. Will porting over my code to select / case statements increase efficiency?
I prefer if statements for non-numeric comparisons and case statements for integral comparisons of large sets. Likely the code slowness is due to algorithm inefficiency than control-flow structure as .NET uses a nifty cheat to make cases work fast on strings. Post some code.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
Most of this sig is for Google, not ego. -
Which is faster? I have a class that loops through consitantly monitoring input and filters down tot he correct path based off multiple factors, I noticed now that my paths that are down the nested if by 7 or 8 layers take as long as 2 seconds to execute. Will porting over my code to select / case statements increase efficiency?
For me, it's a matter of readability, and then suitability of purpose. Heavily nested if statements are generally harder to read than a switch statement. Unless I feel a crying need to use if (less than three nested if's), I'll use a switch statement.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
For me, it's a matter of readability, and then suitability of purpose. Heavily nested if statements are generally harder to read than a switch statement. Unless I feel a crying need to use if (less than three nested if's), I'll use a switch statement.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
Which is faster? I have a class that loops through consitantly monitoring input and filters down tot he correct path based off multiple factors, I noticed now that my paths that are down the nested if by 7 or 8 layers take as long as 2 seconds to execute. Will porting over my code to select / case statements increase efficiency?
A switch is implemented differently depending on how many labels there are in it. If there are more than five (IIRC) labels, it's implemented using a hash table. If there are fewer labels it's implemented using if/else tests, so the earlier labels are the fastest to reach, but when it's implemented using a hash table it takes the same time to reach any of the labels. So, if you have more than five if/else checks on the same value, you should definitely replace it with a switch.
Despite everything, the person most likely to be fooling you next is yourself.
-
So there is no actual efficiency when using a select case over a bunch of if statements?
I don't know if you'd notice an efficiency using either one unless you were going to have more a handful of cases. Most of my switch statements never exceed more than a dozen cases.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
So there is no actual efficiency when using a select case over a bunch of if statements?
EliottA wrote:
So there is no actual efficiency when using a select case over a bunch of if statements?
In your situation, there is no "actual efficiency" in terms of performance using
if
statements vs usingswitch
statements. If your deepest loops are taking 2 seconds to execute, switching fromif
toswitch
may improve your execution time by the tiniest fraction of a millisecond. But that's besides the point. Unless you absolutely need to shave milliseconds off your code, decisions about how your code is organized should be about readability and communicating your intent to future readers of the code. Don't prematurely optimize. That aside, there is more opportunity for a compiler to optimize longswitch
statements than with a series ofif
statements. The compiler will typically convertif
statements into a series of compare-and-jump operations.Switch
statements (in C#) can be implemented using techniques like jump tables (when comparingint
s) or hash tables (when comparingstring
s) to increase code efficiency. But we're talking about real low-level, behind-the-scenes stuff that the average developer will likely never have to consider in real-world applications. Enjoy, Robert C. Cartaino -
A switch is implemented differently depending on how many labels there are in it. If there are more than five (IIRC) labels, it's implemented using a hash table. If there are fewer labels it's implemented using if/else tests, so the earlier labels are the fastest to reach, but when it's implemented using a hash table it takes the same time to reach any of the labels. So, if you have more than five if/else checks on the same value, you should definitely replace it with a switch.
Despite everything, the person most likely to be fooling you next is yourself.
It also means that if you want a fast switch statement, and you have less than five cases, pad the switch statement with do-nothing cases to get the speed gain over the if/else nest.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
It also means that if you want a fast switch statement, and you have less than five cases, pad the switch statement with do-nothing cases to get the speed gain over the if/else nest.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
Which is faster? I have a class that loops through consitantly monitoring input and filters down tot he correct path based off multiple factors, I noticed now that my paths that are down the nested if by 7 or 8 layers take as long as 2 seconds to execute. Will porting over my code to select / case statements increase efficiency?
Hi, for or readability/maintainability the switch is best. for performance it very much depends on the probability distribution. If one or a few cases are much more likely than all the others, use chained if/else for these cases (ordered by descending probability), then handle the remaining stuff with a switch; that way you avoid the switch overhead most of the time. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Fixturized forever. :confused: