aspose file tools*
The moose likes Java in General and the fly likes Switch case vs If elseif Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Switch case vs If elseif" Watch "Switch case vs If elseif" New topic
Author

Switch case vs If elseif

Kowshik Nandagudi
Ranch Hand

Joined: Dec 09, 2010
Posts: 57
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)


Fyodor Sherstobitov
Greenhorn

Joined: Feb 21, 2011
Posts: 9
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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

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.

  • William Brogden
    Author and all-around good cowpoke
    Rancher

    Joined: Mar 22, 2000
    Posts: 12805
        
        5
    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
    Martin Vajsar
    Sheriff

    Joined: Aug 22, 2010
    Posts: 3610
        
      60

    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.
     
     
    subject: Switch case vs If elseif