c++ - 非虚拟成员的虚拟和继承成本?

标签 c++ optimization inheritance virtual

虚拟性可能会产生双重开销:

  • 内存(由于 vptr 和 vtable)
  • 运行速度

由于内存开销,当我需要非常高的内存优化时,我使用一些 CRTP 技术来获得一种静态虚拟性。

但我想知道虚拟化对非虚拟成员的运行速度的成本:

#include <iostream>

class Base
{
    public:
        Base() {;}
        virtual ~Base() {;}

    public:
        virtual void f1() {std::cout<<"f1 : Base"<<std::endl; /* FUNCTION BODY */}
        void f2() {std::cout<<"f2 : Base"<<std::endl; /* FUNCTION BODY */}
        void f3() {f1();}
};

class Derived : public Base
{
    public:
        Derived() {;}
        virtual ~Derived() {;}

    public:
        virtual void f1() {std::cout<<"f1 : Derived"<<std::endl; /* FUNCTION BODY */}
};

主要内容:

int main()
{
    Base b;
    Derived d;
    Base* ptr = new Derived();

    std::cout<<std::endl;
    b.f1(); // 1a
    b.f2(); // 1b
    b.f3(); // 1c
    std::cout<<std::endl;
    d.f1(); // 2a
    d.f2(); // 2b
    d.f3(); // 2c
    std::cout<<std::endl;
    ptr->f1(); // 3a
    ptr->f2(); // 3b
    ptr->f3(); // 3c
    std::cout<<std::endl;

    return 0;
}

对于每种情况:1a、1b ... 3c,与 Base 和 Derived 是两个完全独立的类(没有继承)的情况相比,由于继承+虚拟性,我在哪里有运行时开销(执行时间增加)?

特别是,f2 函数是否有运行时开销?

注意:std::cout 只是一个示例。 /* FUNCTION BODY */ 可以是 1k 行代码...

最佳答案

为什么不直接计时呢?这是一个完全微不足道的练习..

首先,一些结果

100 million instances of b.f1() = 0.774852 secs.
100 million instances of b.f2() = 0.78162 secs.
100 million instances of b.f3() = 1.85278 secs.

100 million instances of d.f1() = 0.773115 secs.
100 million instances of d.f2() = 0.886528 secs.
100 million instances of d.f3() = 1.88562 secs.

100 million instances of ptr->f1() = 1.02043 secs.
100 million instances of ptr->f2() = 0.778072 secs.
100 million instances of ptr->f3() = 1.72503 secs.

假设 win32,(QueryPerformanceXXXXX & LARGE_INTEGER)您可以使用以下内容:

#include <windows.h>
#include <iostream>
using namespace std;

class Base
{
    public:
        Base() {;}
        virtual ~Base() {;}

    public:
        virtual void f1() {};//std::cout<<"f1 : Base"<<std::endl; /* FUNCTION BODY */}
        void f2() {}; //std::cout<<"f2 : Base"<<std::endl; /* FUNCTION BODY */}
        void f3() {f1();}
};

class Derived : public Base
{
    public:
        Derived() {;}
        virtual ~Derived() {;}

    public:
        virtual void f1() {};//std::cout<<"f1 : Derived"<<std::endl; /* FUNCTION BODY */}
};


LARGE_INTEGER clockFreq;

LARGE_INTEGER getTicks()
{
    LARGE_INTEGER result;
    QueryPerformanceCounter(&result);
    return result;
}

double elapsedSecs(LARGE_INTEGER tStart, LARGE_INTEGER tEnd)
{
    long ticksElapsed = tEnd.QuadPart - tStart.QuadPart;
    double timePeriod = (double)ticksElapsed / (double)clockFreq.QuadPart;
    return timePeriod;
}


int main()
{
    LARGE_INTEGER tStart, tEnd;
    Base b;
    Derived d;
    long i, max=100000000;

    Base* ptr = new Derived();

    // find how fast the clock ticks
    QueryPerformanceFrequency(&clockFreq);


/*====================================================================================================
    Test for access using b
    b.f1()
    b.f2()
    b.f3()
====================================================================================================*/
    std::cout<<std::endl;
    tStart = getTicks();
    for (i=0; i<max; i++)
    {
        b.f1(); // 1a
    }
    tEnd = getTicks();
    double elapsed = elapsedSecs(tStart, tEnd);
    cout << "100 million instances of b.f1() = " << elapsed << " secs." << endl;


    std::cout<<std::endl;
    tStart = getTicks();
    for (i=0; i<max; i++)
    {
        b.f2(); // 1a
    }
    tEnd = getTicks();
    elapsed = elapsedSecs(tStart, tEnd);
    cout << "100 million instances of b.f2() = " << elapsed << " secs." << endl;

    std::cout<<std::endl;
    tStart = getTicks();
    for (i=0; i<max; i++)
    {
        b.f3(); // 1a
    }
    tEnd = getTicks();
    elapsed = elapsedSecs(tStart, tEnd);
    cout << "100 million instances of b.f3() = " << elapsed << " secs." << endl;


/*====================================================================================================
    Test for access using d
    d.f1()
    d.f2()
    d.f3()
====================================================================================================*/
    std::cout<<std::endl;
    tStart = getTicks();
    for (i=0; i<max; i++)
    {
        d.f1(); // 1a
    }
    tEnd = getTicks();
    elapsed = elapsedSecs(tStart, tEnd);
    cout << "100 million instances of d.f1() = " << elapsed << " secs." << endl;


    std::cout<<std::endl;
    tStart = getTicks();
    for (i=0; i<max; i++)
    {
        d.f2(); // 1a
    }
    tEnd = getTicks();
    elapsed = elapsedSecs(tStart, tEnd);
    cout << "100 million instances of d.f2() = " << elapsed << " secs." << endl;

    std::cout<<std::endl;
    tStart = getTicks();
    for (i=0; i<max; i++)
    {
        d.f3(); // 1a
    }
    tEnd = getTicks();
    elapsed = elapsedSecs(tStart, tEnd);
    cout << "100 million instances of d.f3() = " << elapsed << " secs." << endl;

/*====================================================================================================
    Test for access using ptr
    ptr->f1()
    ptr->f2()
    ptr->f3()
====================================================================================================*/
    std::cout<<std::endl;
    tStart = getTicks();
    for (i=0; i<max; i++)
    {
        ptr->f1(); // 1a
    }
    tEnd = getTicks();
    elapsed = elapsedSecs(tStart, tEnd);
    cout << "100 million instances of ptr->f1() = " << elapsed << " secs." << endl;


    std::cout<<std::endl;
    tStart = getTicks();
    for (i=0; i<max; i++)
    {
        ptr->f2(); // 1a
    }
    tEnd = getTicks();
    elapsed = elapsedSecs(tStart, tEnd);
    cout << "100 million instances of ptr->f2() = " << elapsed << " secs." << endl;

    std::cout<<std::endl;
    tStart = getTicks();
    for (i=0; i<max; i++)
    {
        ptr->f3(); // 1a
    }
    tEnd = getTicks();
    elapsed = elapsedSecs(tStart, tEnd);
    cout << "100 million instances of ptr->f3() = " << elapsed << " secs." << endl;

    return 0;
}

关于c++ - 非虚拟成员的虚拟和继承成本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12756672/

相关文章:

c++ - 使用 C++ 检索网站数据

c# - 检查类/接口(interface)继承的正确方法是什么?

java - 在Java中,当一个接口(interface) "extends"另一个接口(interface)时到底会发生什么?

c++ - 我如何为类的函数成员进行以下工作?

c++ - g++ 在 freebsd 上包含路径

C++牙签程序

c++ - 为什么这个 += 循环比等价的 = 循环更快?

java - java中if比较的长列表

Python logger.debug 将参数转换为字符串而不记录日志

c# - 如何在 C# 中实现类之间的共享行为(当然没有多重继承)