Lookup table vs switch in C embedded software

前端 未结 6 2151
无人及你
无人及你 2020-12-05 09:28

In another thread, I was told that a switch may be better than a lookup table in terms of speed and compactness.

So I\'d like to understand the

6条回答
  •  不知归路
    2020-12-05 10:27

    First, on some processors, indirect calls (e.g. through a pointer) - like those in your Lookup Table example - are costly (pipeline breakage, TLB, cache effects). It might also be true for indirect jumps...

    Then, a good optimizing compiler might inline the call to func1() in your Switch example; then you won't run any prologue or epilogue for an inlined functions.

    You need to benchmark to be sure, since a lot of other factors matter on the performance. See also this (and the reference there).

提交回复
热议问题