Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Case select versus IF Statement

Case select versus IF Statement

Scheduled Pinned Locked Moved C#
question
10 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    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?

    E realJSOPR G L 4 Replies Last reply
    0
    • L Lost User

      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?

      E Offline
      E Offline
      Ennis Ray Lynch Jr
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • L Lost User

        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?

        realJSOPR Offline
        realJSOPR Offline
        realJSOP
        wrote on last edited by
        #3

        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

        L 1 Reply Last reply
        0
        • realJSOPR realJSOP

          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

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          So there is no actual efficiency when using a select case over a bunch of if statements?

          realJSOPR R 2 Replies Last reply
          0
          • L Lost User

            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?

            G Offline
            G Offline
            Guffa
            wrote on last edited by
            #5

            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.

            realJSOPR 1 Reply Last reply
            0
            • L Lost User

              So there is no actual efficiency when using a select case over a bunch of if statements?

              realJSOPR Offline
              realJSOPR Offline
              realJSOP
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • L Lost User

                So there is no actual efficiency when using a select case over a bunch of if statements?

                R Offline
                R Offline
                Robert C Cartaino
                wrote on last edited by
                #7

                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 using switch statements. If your deepest loops are taking 2 seconds to execute, switching from if to switch 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 long switch statements than with a series of if statements. The compiler will typically convert if statements into a series of compare-and-jump operations. Switch statements (in C#) can be implemented using techniques like jump tables (when comparing ints) or hash tables (when comparing strings) 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

                1 Reply Last reply
                0
                • G Guffa

                  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.

                  realJSOPR Offline
                  realJSOPR Offline
                  realJSOP
                  wrote on last edited by
                  #8

                  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

                  G 1 Reply Last reply
                  0
                  • realJSOPR realJSOP

                    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

                    G Offline
                    G Offline
                    Guffa
                    wrote on last edited by
                    #9

                    No, the if/else implementation is still faster for a switch with very few items.

                    Despite everything, the person most likely to be fooling you next is yourself.

                    1 Reply Last reply
                    0
                    • L Lost User

                      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?

                      L Offline
                      L Offline
                      Luc Pattyn
                      wrote on last edited by
                      #10

                      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:


                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups