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

  • Home
  • SEARCH
  • 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 6648959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:43:02+00:00 2026-05-26T00:43:02+00:00

This question is more or less similar to this Extend an existing API: Use

  • 0

This question is more or less similar to this Extend an existing API: Use default argument or wrapper function?
but sufficiently different.
Please read.

I have a class Foo that has function oldFunc that is used in many parts of my program.
In order to introduce new behaviour to Foo I implemented newFunc and made it private so that
callers to oldFunc keep using the same API but can get the new behaviour if they set a flag by
calling setUseType() function.

Pseudo code follows:

class Foo
{
public:
  Foo(){ useNew = false;}
  void setUseType(bool useType){ useNew = useType;}
  std::string oldFunc(int r) 
  {
     if(useNew )
    {
      return newFunc(r);
    }
    return std::string("Some value after doing some work");
  }
 private:
   std::string newFunc(int r){ return std::string("Some value after doing some work");}
   bool useNew;
};

Question oldFunc is called many times ( millions of times) inside a loop.
How much overhead should I expect from wrapping newFunc inside oldFunc.
Theoretically if oldFunc and newFunc where indentical in implementation is there a reason to expect
calls with a flag setUseType(true) to be slower?

  • 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-05-26T00:43:03+00:00Added an answer on May 26, 2026 at 12:43 am

    In this particular case, there will be no overhead on any non-idiotic compiler as the function will be inlined. Your code would be equivalent to:

    class Foo
    {
    public:
      Foo(){ useNew = false;}
      void setUseType(bool useType){ useNew = useType;}
      std::string oldFunc(int r) 
      {
         if(useNew )
        {
          return std::string("Some value after doing some work");//newFunc inlined here
        }
        return std::string("Some value after doing some work");
      }
     private:
       bool useNew;
    };
    

    If it were not inlined, there would be an extra jump for the call to the new function. That’s where all the extra computation time goes into. This extra jump will most likely cause a lot less delay, even considering millions of calls. I suggest you do some profiling if you really do have speed issues and figure out the real bottleneck – most likely not the extra call.

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

Sidebar

Related Questions

This post is intended to be less of a question and more a confirmation
I asked a question similar to this one a couple of weeks ago, but
So, I found a similar question here , but the answers are more about
This question is more UI/Design-ish than hard-core programming is. Background: I've been coding in
EDIT: This question is more about language engineering than C++ itself. I used C++
I had asked this question Adding more attributes to LINQ to SQL entity Now,
I suppose this question is more general than working with COM components. I have
This is a code review question more then anything. I have the following problem:
This is related to my previous question More than 1 Left joins in MSAccess
This question was asked to me in an interview. Suppose char *p=malloc(n) assigns more

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.