Switch cases maximum implementation?

前端 未结 7 1658
没有蜡笔的小新
没有蜡笔的小新 2021-01-19 12:27

I am using a single switch cases which will have more than 100 cases statement to be used. Are there any limit ?

The usage of cases are for the suggestions of my Aut

7条回答
  •  隐瞒了意图╮
    2021-01-19 12:51

    When implementing switches many optimisations can be made for performance, otherwise you have to list through all the switches till it matches.

    What I would do here, is have a main set of switches for the first character then nested switches inside, therefore if the choice was z it doesn't have to loop check every name first

    switch(FIRSTCHAR){
    case A: switch(index){
            case 0: ..... break;
            etc
    
            }
    break;
    
    case B://do the same
    }
    

    Another way is to break your switch statement up into smaller equal sized statements. This is faster due the way the bytecode is compiled (ref Java performance tuning - shirazi)

提交回复
热议问题