Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8295211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:30:22+00:00 2026-06-08T14:30:22+00:00

I have a class with an enum member variable. One of the member functions

  • 0

I have a class with an enum member variable. One of the member functions bases its behavior on this enum so as a “possible” optimization, I have the two different behaviors as two different functions and I give the class a member function pointer which is set at construction. I simulated this situation like this:

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 traverse the array to bring it into cache, then I call cats update method 32 times and store the latency using rdtsc in the array…

Then I call a function which loops several hundred times using rand(), ulseep(), and some arbitrary strcmp()..come back and I do the 32 thing again.

The result is that the method with the branch seems to always be around 44 +/- 10 cycles whereas the one with the function pointer tends to be around 130. I’m curious as to why this would be the case?

If anything, I would have expected similar performance. Also, templating is hardly an option because full specialization of the real cat class for that one function would be overkill.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T14:30:29+00:00Added an answer on June 8, 2026 at 2:30 pm

    Without a complete SSCCE I can’t approach this the same way that I usually do with such questions.
    So the best I can do is speculate:

    The core difference between your two cases is that you have a branch vs. a function pointer. The fact that you are seeing a difference at all strongly hints that funcA() and funcB() are very small functions.

    Possibility #1:

    In the branch version of the code, funcA() and funcB() are being inlined by the compiler. Not only does that skip the function call overhead, but if the functions are trivial enough, the branch could also be completely optimized out as well.

    Function pointers, on the other hand, cannot be inlined unless the compiler can resolve them at compile-time.

    Possibility #2:

    By comparing a branch against a function-pointer, you are putting the branch-predictor against the branch target predictor.

    Branch target prediction is not the same as branch prediction. In the branch case, the processor needs to predict which way to branch. In the function pointer case, it needs to predict where to branch to.

    It’s very likely that your processor’s branch predictor is much more accurate than its branch target predictor. But then again, this is all guesswork…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that defines its own enum like this: public class Test
I have a class with a private member ( struct ) as follows: enum
I have an enum class called Gender , and I have values in this
In java I have: public class MyClass{ public enum STATUS { ZERO, ONE ,
I have an enum value as a member of a class which I want
If I have a class with an enum member and I want to be
I have this enum (class) enum class conditional_operator { plus_op, or_op, not_op } And
I have a class that has a const member, const pointer and enum class
I have a class I've written to act like an enumeration: abstract class Enum
I have the following class: class Enum(RootFragment): def __init__(self, column, packageName, name, modifiers=[public], enumValues=[]):

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.