2

仮想性には2つのオーバーヘッドがあります。

  • メモリ(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が継承のない2つの完全に独立したクラスである場合と比較して、継承と仮想化による実行時のオーバーヘッド(実行時間の増加)はどこにありますか?

特に、f2関数の実行時のオーバーヘッドはありますか?

注:std::coutこれは単なる例です。/* FUNCTION BODY */1k行のコードにすることができます...

4

1 に答える 1

4

時間を計ってみませんか?それは完全に些細なことです。

まず、いくつかの結果

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;
}
于 2012-10-06T05:11:19.143 に答える