My bet would be on the second version the fastest with if/else
outside the loop, provided that I get a refund when we link and test it in the widest range of compilers.: - D I have been making this bet with quite a lot of years since VTune in the hand.
However, I would be happy if I lost the bet. I think it is very possible that many compilers can now optimize the first version to compete with the second, finding that you repeatedly check a variable that does not change inside the loop, and therefore effectively raises branching that occurs outside the loop.
However, I have not yet met the case when I saw that the optimizer performs the same equivalent of embedding an invocation of an indirect function ... although, if there were a case where the optimizer could do this, then your definitely would be the easiest, as it assigns addresses of functions to call in the same function in which it calls these functions through function pointers. I would be very pleasantly surprised if optimizers can do it now, especially because I like your third version in terms of maintenance (the easiest way is to change if we want to add new conditions that lead to different functions for calling, for example).
Nevertheless, if it is not embedded, then the solution of the function pointer will tend to be the most expensive not only because of the long jump, but also, possibly, additional stack leaks, etc., as well as the lack of an optimizer. There is an optimizer barrier when it does not know which function is called through the pointer. At this point, he can no longer combine all this information in IR and do the best job of selecting commands, register allocation, etc. This aspect of the layout for indirect function calls is not discussed often, but is potentially the most expensive part of a function call indirectly.
source share