title: Why virtual function call is faster than dynamic_cast?
body: <p>I wrote a simple example, which estimates average time of calling virtual function, using base class interface and dynamic_cast and call of non-virtual function.
  Here is it:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;numeric&gt;
  #include &lt;list&gt;
  #include &lt;time.h&gt;
  
  #define CALL_COUNTER (3000)
  
  __forceinline int someFunction()
  {
    return 5;
  }
  
  struct Base
  {
    virtual int virtualCall() = 0;
    virtual ~Base(){};
  };
  
  struct Derived : public Base
  {
    Derived(){};
    virtual ~Derived(){};
    virtual int virtualCall(){ return someFunction(); };
    int notVirtualCall(){ return someFunction(); };
  };
  
  
  struct Derived2 : public Base
  {
    Derived2(){};
    virtual ~Derived2(){};
    virtual int virtualCall(){ return someFunction(); };
    int notVirtualCall(){ return someFunction(); };
  };
  
  typedef std::list&lt;double&gt; Timings;
  
  Base* createObject(int i)
  {
    if(i % 2 &gt; 0)
      return new Derived(); 
    else 
      return new Derived2(); 
  }
  
  void callDynamiccast(Timings&amp; stat)
  {
    for(unsigned i = 0; i &lt; CALL_COUNTER; ++i)
    {
      Base* ptr = createObject(i);
  
      clock_t startTime = clock();
  
      for(int j = 0; j &lt; CALL_COUNTER; ++j)
      {
        Derived* x = (dynamic_cast&lt;Derived*&gt;(ptr));
        if(x) x-&gt;notVirtualCall();
      }
  
      clock_t endTime = clock();
      double callTime = (double)(endTime - startTime) / CLOCKS_PER_SEC;
      stat.push_back(callTime);
  
      delete ptr;
    }
  }
  
  void callVirtual(Timings&amp; stat)
  {
    for(unsigned i = 0; i &lt; CALL_COUNTER; ++i)
    {
      Base* ptr = createObject(i);
  
      clock_t startTime = clock();
  
      for(int j = 0; j &lt; CALL_COUNTER; ++j)
        ptr-&gt;virtualCall();
  
  
      clock_t endTime = clock();
      double callTime = (double)(endTime - startTime) / CLOCKS_PER_SEC;
      stat.push_back(callTime);
  
       delete ptr;
    }
  }
  
  int main()
  {
    double averageTime = 0;
    Timings timings;
  
  
    timings.clear();
    callDynamiccast(timings);
    averageTime = (double) std::accumulate&lt;Timings::iterator, double&gt;(timings.begin(), timings.end(), 0);
    averageTime /= timings.size();
    std::cout &lt;&lt; "time for callDynamiccast: " &lt;&lt; averageTime &lt;&lt; std::endl;
  
    timings.clear();
    callVirtual(timings);
    averageTime = (double) std::accumulate&lt;Timings::iterator, double&gt;(timings.begin(), timings.end(), 0);
    averageTime /= timings.size();
    std::cout &lt;&lt; "time for callVirtual: " &lt;&lt; averageTime &lt;&lt; std::endl;
  
    return 0;
  }
  </code></pre>
  
  <p>It looks like callDynamiccast takes almost two times more.</p>
  
  <p><code>time for callDynamiccast: 0.000240333</code></p>
  
  <p><code>time for callVirtual: 0.0001401</code></p>
  
  <p>Any ideas why does it?</p>
  
  <p>EDITED: object creation is made in separete function now, so the compler does not know it real type. Almost the same result.</p>
  
  <p>EDITED2: create two different types of a derived objects.</p>