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


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

Re: Non virtual and inheritance

From Juha Nieminen <nospam@thanks.invalid>
Newsgroups comp.lang.c++
Subject Re: Non virtual and inheritance
Date 2012-10-19 09:39 +0000
Organization Aioe.org NNTP Server
Message-ID <k5r756$cmd$1@speranza.aioe.org> (permalink)
References <k5oqnl$slk$1@speranza.aioe.org> <pan.2012.10.18.12.29.32.390000@nowhere.com>

Show all headers | View raw


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).

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. In
most cases this doesn't matter, but if you are super-tightly optimizing
the memory usage of a very small class, that size increase can be crucial.
(If eg. your class only has eg. an integral as a member, making any of the
functions virtual will double the size of the class.)

(Virtuality may also impede or reduce the effectiveness of function
inlining, but that's also something that is relevant only in certain
cases.)

This overhead is useless if you don't need virtual functions for anything.

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