How can Duff's device code be compiled?

前端 未结 5 1492
既然无缘
既然无缘 2020-12-10 17:21

I understood why Duff\'s device is faster than normal loop code which can be unrolled but is not optimized. But I can\'t understand how the code can be compiled yet.
I g

5条回答
  •  醉梦人生
    2020-12-10 17:41

    While it's true that Duff's Device is outdated for its original purpose, it's still useful for special purposes, like a state machine that normally cycles repeatedly through N states, but sometimes needs to return to the caller and later be resumed at the state where it left off. Putting the switch statement outside the loop and the case labels inside the loop (I would take this as the definition of a "Duff's device") then makes a lot of sense.

    With that said, do not use Duff's devices to "optimize by hand". Putting what are effectively "goto labels" all over the place will not help the compiler optimize.

提交回复
热议问题