Is the conditional operator slow?

前端 未结 8 1958
广开言路
广开言路 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:39

    I would expect #1 and #2 to be the same. The optimizer should result in the same code. The dictionary in #3 would expected to be slow, unless it is optimized somehow to not actual use a hash.

    When coding real-time systems, we always used a look-up table--a simple array--to translate as given in your example. It's the fastest when the range of input is fairly small.

提交回复
热议问题