C++ : difference of execution time between two call of a virtual function

标签 c++ performance function virtual c++11

gcc 4.5.1 (Ubuntu 10.04, intel core2duo 3.0 Ghz)下考虑这段代码 这只是 2 个测试,第一个是我直接调用 virtual fucnion,第二个是通过 Wrapper 类调用它:

测试.cpp

#define ITER 100000000

class Print{

public:

typedef Print* Ptr;

virtual void print(int p1, float p2, float p3, float p4){/*DOES NOTHING */}

};

class PrintWrapper
{

    public:

      typedef PrintWrapper* Ptr;

      PrintWrapper(Print::Ptr print, int p1, float p2, float p3, float p4) :
      m_print(print), _p1(p1),_p2(p2),_p3(p3),_p4(p4){}

      ~PrintWrapper(){}

      void execute()
      { 
        m_print->print(_p1,_p2,_p3,_p4); 
      }

    private:

      Print::Ptr m_print;
      int _p1;
      float _p2,_p3,_p4;

};

 Print::Ptr p = new Print();
 PrintWrapper::Ptr pw = new PrintWrapper(p, 1, 2.f,3.0f,4.0f);

void test1()
{

 //-------------test 1-------------------------

 for (auto var = 0; var < ITER; ++var) 
 {
   p->print(1, 2.f,3.0f,4.0f);
 }

 }

 void test2()
 {

  //-------------test 2-------------------------

 for (auto var = 0; var < ITER; ++var) 
 {
   pw->execute();
 }

}

int main() 
{ 
  test1(); 
  test2();
}

我用 gprof 和 objdump 分析了它:

g++ -c -std=c++0x -pg -g -O2 test.cpp
objdump -d -M intel -S test.o > objdump.txt
g++ -pg test.o -o test
./test
gprof test > gprof.output

在 gprof.output 中,我观察到 test2() 比 test1() 花费更多时间,但我无法解释它

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls  ms/call  ms/call  name    
 49.40      0.41     0.41        1   410.00   540.00  test2()
 31.33      0.67     0.26 200000000     0.00     0.00  Print::print(int, float, float, float)
 19.28      0.83     0.16        1   160.00   290.00  test1()
  0.00      0.83     0.00        1     0.00     0.00  global constructors keyed to p

objdump.txt 中的汇编代码对我也没有帮助:

 //-------------test 1-------------------------
 for (auto var = 0; var < ITER; ++var) 
  15:   83 c3 01                add    ebx,0x1
 {
   p->print(1, 2.f,3.0f,4.0f);
  18:   8b 10                   mov    edx,DWORD PTR [eax]
  1a:   c7 44 24 10 00 00 80    mov    DWORD PTR [esp+0x10],0x40800000
  21:   40 
  22:   c7 44 24 0c 00 00 40    mov    DWORD PTR [esp+0xc],0x40400000
  29:   40 
  2a:   c7 44 24 08 00 00 00    mov    DWORD PTR [esp+0x8],0x40000000
  31:   40 
  32:   c7 44 24 04 01 00 00    mov    DWORD PTR [esp+0x4],0x1
  39:   00 
  3a:   89 04 24                mov    DWORD PTR [esp],eax
  3d:   ff 12                   call   DWORD PTR [edx]

  //-------------test 2-------------------------
 for (auto var = 0; var < ITER; ++var) 
  65:   83 c3 01                add    ebx,0x1

      ~PrintWrapper(){}

      void execute()
      { 
        m_print->print(_p1,_p2,_p3,_p4); 
  68:   8b 10                   mov    edx,DWORD PTR [eax]
  6a:   8b 70 10                mov    esi,DWORD PTR [eax+0x10]
  6d:   8b 0a                   mov    ecx,DWORD PTR [edx]
  6f:   89 74 24 10             mov    DWORD PTR [esp+0x10],esi
  73:   8b 70 0c                mov    esi,DWORD PTR [eax+0xc]
  76:   89 74 24 0c             mov    DWORD PTR [esp+0xc],esi
  7a:   8b 70 08                mov    esi,DWORD PTR [eax+0x8]
  7d:   89 74 24 08             mov    DWORD PTR [esp+0x8],esi
  81:   8b 40 04                mov    eax,DWORD PTR [eax+0x4]
  84:   89 14 24                mov    DWORD PTR [esp],edx
  87:   89 44 24 04             mov    DWORD PTR [esp+0x4],eax
  8b:   ff 11                   call   DWORD PTR [ecx]

我们如何解释这种差异?

最佳答案

test2()中,程序必须首先从堆中加载pw,然后调用pw->execute()(这会导致调用开销),然后加载 pw->m_print 以及 _p1_p4 参数,然后加载 pw 的 vtable 指针,然后为pw->Print加载vtable槽,然后调用pw->Print。因为编译器无法看穿虚拟调用,所以它必须假定所有这些值在下一次迭代中都已更改,并重新加载它们。

test()中,参数在代码段中内联,我们只需要加载p、vtable指针和vtable槽。我们以这种方式节省了五次负载。这很容易解释时差。

简而言之 - pw->m_printpw->_p1pw->_p4 的负载是这里的罪魁祸首。

关于C++ : difference of execution time between two call of a virtual function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8405937/

相关文章:

C++ 将 uint8_t 转换为 unsigned long

bash - bash 上的嵌套函数

javascript - 使用 JavaScript 操作多个(可预测的)命名 DIV

javascript - 这两个比较有什么区别?

c++ - 使用未声明的标识符 'gl_LightSource'

c++ - 如何在 OSX 上构建 FlatBuffers ?

c++ - 在 C++ 中,如何让用户在应用程序窗口外指定屏幕上的一个点 (X,Y)?

c++ - 在 C++ 中,如何声明一个只能在外部更改的类成员?

java - Swing 如何处理对同一组件的连续更改?

performance - Boost Graph 识别顶点