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
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.