What is the effect of ordering if…else if statements by probability?

后端 未结 10 1810
花落未央
花落未央 2020-12-07 13:39

Specifically, if I have a series of if...else if statements, and I somehow know beforehand the relative probability that each statement will evalua

10条回答
  •  甜味超标
    2020-12-07 14:11

    If you already know the relative probability of if-else statement,then for performance purpose it would be better to use the sorted way, as it will only check one condition(the true one).

    In an unsorted way the compiler will check all the conditions unnecessarily and will take time.

提交回复
热议问题