Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #19087

Re: Non virtual and inheritance

From Nobody <nobody@nowhere.com>
Subject Re: Non virtual and inheritance
Date 2012-10-19 15:01 +0100
Message-Id <pan.2012.10.19.14.01.58.787000@nowhere.com>
Newsgroups comp.lang.c++
References <k5oqnl$slk$1@speranza.aioe.org> <pan.2012.10.18.12.29.32.390000@nowhere.com> <k5r756$cmd$1@speranza.aioe.org>
Organization Zen Internet

Show all headers | View raw


On Fri, 19 Oct 2012 09:39:50 +0000, Juha Nieminen wrote:

> Nobody <nobody@nowhere.com> wrote:
>> Virtual calls are more computationally expensive
> 
> Actually if you actually measure the speed difference between calling
> a regular (non-inlined) function and a virtual function, the difference
> is barely measurable, if not inexistent. Yes, there's an additional
> indirection step, but its impact on the overall speed of the function
> call is quite minimal (especially if the function takes parameters and
> does something that takes many clock cycles).

It wasn't always thus. CPUs were rather more naive when C++ was originally
designed. But the biggest issue is that virtual typically inhibits
inlining, which has an impact far beyond the cost of a function call.

> In short: If you are avoiding virtual functions because you fear that
> your program will become slower, then you are worrying for nothing.
> 
> The actual problem with virtual functions (which is only a problem in
> certain cases) is that a class having virtual functions will be larger
> (typically by a pointer) than a class with no virtual functions.

If the vptr is a problem, it isn't usually due to the size, but to making
the type non-POD. E.g. being unable to pass such objects between processes
using shared memory, or efficient I/O via mmap(), or making the memory
layout conform to a pre-defined format.

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Non virtual and inheritance Sensei <sensei_s_mail_is_at@me.com> - 2012-10-18 13:55 +0200
  Re: Non virtual and inheritance Nobody <nobody@nowhere.com> - 2012-10-18 13:29 +0100
    Re: Non virtual and inheritance Juha Nieminen <nospam@thanks.invalid> - 2012-10-19 09:39 +0000
      Re: Non virtual and inheritance Nobody <nobody@nowhere.com> - 2012-10-19 15:01 +0100
        Re: Non virtual and inheritance Juha Nieminen <nospam@thanks.invalid> - 2012-10-19 16:01 +0000
  Re: Non virtual and inheritance Rui Maciel <rui.maciel@gmail.com> - 2012-10-18 14:49 +0100
  Re: Non virtual and inheritance Luca Risolia <luca.risolia@studio.unibo.it> - 2012-10-18 20:06 +0200
  Re: Non virtual and inheritance Paavo Helde <myfirstname@osa.pri.ee> - 2012-10-18 14:40 -0500
  Re: Non virtual and inheritance Juha Nieminen <nospam@thanks.invalid> - 2012-10-19 09:41 +0000

csiph-web