I have the following loop which calls all function pointers in an array:
for(auto f : program) {
f();
}
I'd like to optimize this. So far, I've tried two methods:
- Tail recursion
- JITting threaded code
Here is the complete test code: https://coliru.stacked-crooked.com/a/d639f024b1222c54
The timing results on my machine (iMac Pro 8-Core) are:
naive: 0.530534
tail recursion: 0.265192
JIT threaded: 0.125106
Of course the functions all have to be modified to facilitate tail recursion, but that's ok. What would be less pleasant in terms of code cleanliness would be to put everything in one function and use something like computed goto (I've tried that too, actually, and computed goto is only slightly faster than tail recursion on my machine.)
Can I do better than tail recursion without JITting? (on iOS, JITting is not allowed)
Note that the functions cannot be re-ordered.