Is the conditional operator slow?

前端 未结 8 1932
广开言路
广开言路 2020-12-03 06:59

I was looking at some code with a huge switch statement and an if-else statement on each case and instantly felt the urge to optimize. As a good developer always should do I

8条回答
  •  一向
    一向 (楼主)
    2020-12-03 07:36

    I don't quite understand why you would expect an if statement to be slower than a dictionary lookup. At the very least a hashcode needs to be calculated and then it needs to be looked up in a list. I don't see why you would assume this is faster than a cmp/jmp.

    Specifically, I don't even think the method you're optimising is that great; it seems that it could be made better at the calling stage (though I can't be sure, as you haven't provided the context).

提交回复
热议问题