2

Is it possible to create a new "Custom Conditional Statement" in java.

Here i am planning to create a new custom component for the switch statement to give better performance. The custom Switch control which i am planning to create will have only integer operators: example:

int i = SOMEVALUE;
Switch(i)
{
Case 1: 
//Some Statement
Break;
Case 2: 
//Some Statement
Break;
Case 3: 
//Some Statement
Break;
....
....
....
Case 10000000: 
//Some Statement
Break;
Default:
//Some Statement
Break;
}

According to my knowledge the Switch condition will check in a particular order. If the actual value of the "i" variable is 9999999 then it has to check the case for 9999999 times but if we check it with some algorithms similar to binary search then its performance will be improved. Am I Right ??

If right then why don't we add this functionality to our programming language ? So why don't we create a "Custom Conditional Statement" something like "OrderedIntegerSwitch statement" , in which the value passed inside that switch should be an integer.

4 Answers4

7

You're not really right. A switch statement is typically not implemented as a series of if/elseif, but with a jump table, therefore it already executes in constant time, not in linear time. (This is why many languages already constrain the argument to switch to a smaller integral type than theoretically possible.)

To be sure, no compiler will generate a jump table with 1000000 entries if you switch over something that large, but there are similar tricks to get O(1) performance even if the naive approach would use too much space. Modern compilers almost always use some of these. Therefore it's unlikely you would be able to beat your language implementor with a homebrew solution.

Kilian Foth
  • 110,899
5

Besides the fact that compilers are already smart, do you really intend to type ten millions switch cases and their associated ten millions statements, and then hack a compiler for optimizing all of that?

You'd better put your efforts in changing your design. I can't imagine a real life situation where you need ten millions actions that are different enough not to be factorized.

mouviciel
  • 15,491
5

According to the JVM spec, a switch statement can be compiled to the bytecode instructions tableswitch or lookupswitch, depending on wether the cases of the switch are sparse.

tableswitch is O(1), while lookupswitch seems to be O(log n) (I guess it uses a binary search)

If you are curious about what instruction is chosen by your compiler for your piece of code, just decompile the bytecode and see if you find one instruction the other.

barjak
  • 1,740
1

Correct me if I'm wrong, but as far as I know the big difference between if..else statements and the switch ones, is that the first will check for each case one by one, until it finds the one that matches.

With a switch, followed by a break, you will instantly jump to the right case and end the statement afterwards.

I think what you say is true for the if..else statement, but not for the switch, which should be always faster with a large quantity of cases to check for.

Jose Faeti
  • 2,815