Best Practice on IF/ELSE Statement Order

前端 未结 10 2229
陌清茗
陌清茗 2020-12-16 14:48

Which is a better practice? (I\'m coding in .Net if that makes a difference)

IF condition = true THEN
   ...true action--even if rare...
ELSE
   ...action
E         


        
10条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-16 15:36

    In the final assembly/machine code, it does make a difference. The statement which is most likely to be executed is done in the path without the branch. This way, the pipeline isn't broken causing valuable cycles to be lost.

    I have no clue if the compiler leaves your if then statement order intact and this way forces the assembly to take this optimized route.

    I have read that visual studio 2008 (when it was announced) would have an optimization functionality where the compiler adds measurements at branches and then during runtime measures how often a certian path is taken. Then in subsequent recompiles the most optimal code path is preferred.

    I have no clue if this feature ever made it past the 'design/academic phase'

提交回复
热议问题