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 884167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:42:40+00:00 2026-05-15T12:42:40+00:00

I set up a (perhaps very unscientific) small test to determine the overhead of

  • 0

I set up a (perhaps very unscientific) small test to determine the overhead of virtual functions in a one-level single inheritance and the results I got were, well, exactly the same when accessing the derived class polymorphically or when accessing it directly. What was a bit surprising was the order of magnitude of computation time that is introduced when any function is declared virtual (see results below).

Is there so much overhead when declaring member functions as such, and why is it still present even when accessing the derived class directly?

The code is as follows:

class base
{
public:
    virtual ~base() {}
    virtual uint func(uint i) = 0;
};

class derived : public base
{
public:
    ~derived() {}
    uint func(uint i) { return i * 2; }
};

uint j = 0;
ulong k = 0;
double l = 0;
ushort numIters = 10;
base* mybase = new derived;  // or derived* myderived = ...

for(ushort i = 0; i < numIters; i++)
{
  clock_t start2, finish2;
  start2 = clock();

  for (uint j = 0; j < 100000000; ++j)
        k += mybase->func(j);

  finish2 = clock();
  l += (double) (finish2 - start2);
  std::cout << "Total duration: " << (double) (finish2 - start2) << " ms." << std::endl;

}

std::cout << "Making sure the loop is not optimized to nothing: " << k << std::endl;
std::cout << "Average duration: " << l / numIters << " ms." << std::endl;

Results:

base* mybase = new derived; gives an average of ~338 ms.

derived* myderived = new derived; gives an average of ~338 ms.

Eliminating inheritance and removing virtual functions gives an average of ~38 ms.

That’s almost 10 times less! So basically, if any function is declared virtual the overhead will always be identically present, even if I don’t use it polymorphically?

Thanks.

  • 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-15T12:42:40+00:00Added an answer on May 15, 2026 at 12:42 pm

    Accessing it “directly” is doing the same work as accessing it “indirectly”.

    When you call the function on myderived, the pointer stored there could point to some object of some class derived from derived. The compiler can’t assume that it really is a derived object, it might be an object of a further derived class that overrides the virtual function, so there needs to be virtual function dispatch just like in the mybase case. In both cases the function is looked up in the virtual function table before it is called.

    To call the function non-polymorphically, don’t use a pointer:

    derived myderived;
    myderived.func(1); 
    

    When you remove the virtual functions, the compiler can inline the function call so that you basically end up with a simple loop:

    for (uint j = 0; j < 100000000; ++j)
        k += i * 2;
    

    This is much faster since you save the overhead of 100000000 function calls and the compiler might even be able to optimize the loop further in ways it wouldn’t if there was a function call in it.

    Note also that the difference between the inlined version and the virtual function call would be much less if the function did some real work. In this example the function body takes almost no time at all, so the costs for calling the function outweigh the costs for executing the body.

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

Sidebar

Related Questions

I could use another set of eyes - perhaps I am missing somehting obvious.
Perhaps a fresh set of eyes could help me with this. I thought this
set batVar_Parameters=/Developer /Test=0 if not '%batVar_Iteration%' equ '-1' ( set batVar_Parameters=%batVar_Parameters% /Iteration=%batVar_Iteration% ) if
One of my projects on Linux uses blocking sockets. Things happen very serially so
I'm building a small site framework for a set of sites that are likely
As of yesterday (perhaps after a recent PHP update?), I'm getting some very strange
This is perhaps a very simple question but the help file doesn't seem to
I'm using selenium in my test suite, but since it's slow to set up,
So I have a very large project which when run prints a data set
Sorry to bother - perhaps this is a very simple question - but for

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.