I am interested in implementing a state machine in C. I came across a few possible approaches:
- Using goto: /a/133363
- Using switch: /a/1648098
- Using function pointers: /a/1647679/18043162
Now of course, the proper thing to do would be to try each approach and benchmark. But I am interested in perspectives from those familiar with compiler optimisations: what are the likely pros and cons for each in terms of code generation? Would a function-pointer approach tend to be slower than the corresponding goto/switch approach as we have indirection in pointer chasing? Readability aside, is a particular approach more performant/amenable to optimisation?
Thanks.