• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Switch case vs If elseif

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have a program where in I have a large number of if elseif statements comparing the strings . This structure keeps on increasing as I add a new functionality.

Note: this if elseif is inside a for loop

Since java does not allow switch on Strings , I thought of using a Map<String,Integer> mapping my string to number and then using switch case.

But before performing this. I thought does switch case really enhance the performance. ( If there is not much of performance difference I would rather keep the code as it is as the entire project structure will change)


 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, that in case of large amount of variants in conditional loop, the switch/case variant is preffered. For example, you have 100 variants to choose from. You are entering your for loop and looking the first if statement is true or false. If it is false, you are looking the second one, and then the third and then... Until the condition is true. In the worst case this will be 100 computations. Now lets see, what we have in case/switch statement. You are entering the for loop, comparing your number with switch condition and just jumping to the piece of code you need. In the worst case this will be 100 times faster, than with if/else construction. In the best case it will be the same.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It might indeed be 100 times faster. But often there isn't any point in focusing on some code which represents a tiny fraction of your application's run time and trying to make it faster. Even if you made it take no time at all that would only improve the run time by a tiny fraction.

Of course we have no idea whether the code you describe is significant or not. I suspect it isn't, but really I know nothing about it. And neither does anybody else reading your post. So nobody could possibly say whether changing it would make any significant difference at all. I would leave it alone until two things happen:

  • You decide the performance of your application needs to be improved.
  • You determine, by profiling the appliction, that this piece of code is a significant part of the problem.

  •  
    Author and all-around good cowpoke
    Posts: 13078
    6
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    I thought of using a Map<String,Integer> mapping my string to number and then using switch case.



    I have done this a lot and it works just fine. It makes for fairly readable code and is easy to expand.

    Bill
     
    Sheriff
    Posts: 3837
    66
    Netbeans IDE Oracle Firefox Browser
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    I thought of using a Map<String,Integer> mapping my string to number and then using switch case.


    This concept could be taken one step further: use instances of Runnable to perform the functionality and create the map as Map<String, Runnable>. Then you'll obtain instance to perform your action right from the map, so you can just execute map.get(code).run(); (well, a test that the code was in the map would actually be needed). If you need to have parameters and/or return value, create your own interface to represent your actions.

    I would not advice to do this to save the switch execution time, it would clearly be an overkill, but this concept could be more manageable than a switch, depending on your needs.
     
    reply
      Bookmark Topic Watch Topic
    • New Topic