Note that there are some explanatory texts on larger screens.

plurals
  1. POCost of virtuality and inheritance for non virtual members?
    primarykey
    data
    text
    <p>The virtuality can have a double overhead :</p> <ul> <li>memory (because of vptr and vtable)</li> <li>runtime speed</li> </ul> <p>Because of the memory overhead, I use some CRTP techniques to have a kind of static virtuality, when I need very high memory optimization.</p> <p>But I wonder about the cost of virtuality for the runtime speed for non-virtual members :</p> <pre><code>#include &lt;iostream&gt; class Base { public: Base() {;} virtual ~Base() {;} public: virtual void f1() {std::cout&lt;&lt;"f1 : Base"&lt;&lt;std::endl; /* FUNCTION BODY */} void f2() {std::cout&lt;&lt;"f2 : Base"&lt;&lt;std::endl; /* FUNCTION BODY */} void f3() {f1();} }; class Derived : public Base { public: Derived() {;} virtual ~Derived() {;} public: virtual void f1() {std::cout&lt;&lt;"f1 : Derived"&lt;&lt;std::endl; /* FUNCTION BODY */} }; </code></pre> <p>And the main :</p> <pre><code>int main() { Base b; Derived d; Base* ptr = new Derived(); std::cout&lt;&lt;std::endl; b.f1(); // 1a b.f2(); // 1b b.f3(); // 1c std::cout&lt;&lt;std::endl; d.f1(); // 2a d.f2(); // 2b d.f3(); // 2c std::cout&lt;&lt;std::endl; ptr-&gt;f1(); // 3a ptr-&gt;f2(); // 3b ptr-&gt;f3(); // 3c std::cout&lt;&lt;std::endl; return 0; } </code></pre> <p>For each case : 1a, 1b ... 3c, where do I have a runtime overhead (increase of execution time) due to inheritance+virtuality compared to the case where Base and Derived are two completely independant classes with no inheritance ?</p> <p>And particularly, is there any runtime overhead for the f2 function ?</p> <p>Note : <code>std::cout</code> is just an example. <code>/* FUNCTION BODY */</code> can be 1k lines of code...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload