I have a class with enum variable. One of the member functions bases its behavior on this enum so that the βpossibleβ optimization, I have two different behaviors as two different functions, and I give the class a pointer to the member function that is set during construction. I modeled this situation as follows:
enum catMode {MODE_A, MODE_B}; struct cat { cat(catMode mode) : stamp_(0), mode_(mode) {} void update() { stamp_ = (mode_ == MODE_A) ? funcA() : funcB(); } uint64_t stamp_; catMode mode_; }; struct cat2 { cat2(catMode mode) : stamp_(0), mode_(mode) { if (mode_ = MODE_A) func_ = funcA; else func_ = funcB; } void update() { stamp_ = func_(); } uint64_t stamp_; catMode mode_; uint64_t (*func_)(void); };
And then I create a cat object and an array of length 32 . I move the array to cache it, then I call the cats update method 32 and save the delay using rdtsc in the array ...
Then I call a function that loops several hundred times using rand() , ulseep() and some arbitrary strcmp() .. strcmp() back and I do 32 again.
As a result, a method with a branch will always be around 44 +/- 10 loops, while a method with a function pointer tends to be around 130 . I am curious why this will be so?
Anyway, I would expect the same performance. In addition, templates are hardly possible, because the full specialization of the real cat class for this one function will be redundant.
source share