Is performance gained when using continue in a for-loop with many if-statements?

后端 未结 4 1643
小蘑菇
小蘑菇 2021-01-19 11:12

I have a for loop in a java program which iterates through a set of maps.

Inside the loop I have around 10 different if-statements which checks the name of each key

4条回答
  •  伪装坚强ぢ
    2021-01-19 11:25

    With your current implementation, yes you are gaining a performance boost by skipping the remaining if statements using the continue keyword, although with only a constant of ten "if" statements, it's not that bad (10n = O(n) time). Having said that, the more practical way to approach this, as Eran stated, is to make use of else if statements, which will achieve the same result that you are currently using.

提交回复
热议问题