原始类型比C ++中的用户类型慢?

时间:2012-06-06 12:36:49

标签: c++ performance profiling primitive-types usertype

我很好奇并做了一些基准来确定原始类型(如intfloat和用户类型)之间的性能差异。

我创建了一个模板类Var,创建了一些内联算术运算符。测试包括为原始和Var向量循环此循环:

for (unsigned i = 0; i < 1000; ++i) {
    in1[i] = i;
    in2[i] = -i;
    out[i] = (i % 2) ? in1[i] + in2[i] : in2[i] - in1[i];
}

我对结果感到非常惊讶,事实证明我的Var类在大多数情况下都是更快的,平均而言,这个循环在类中减少了大约5700纳秒。在3000次运行中,int比Var快了11倍,快了2989次。与float类似的结果,其中Var比runin 2991快15100纳秒。

原始类型不应该更快吗?

编辑:编译器是一个相当古老的mingw 4.4.0,构建选项是QtCreator的默认值,没有优化:

qmake call: qmake.exe C:\...\untitled15.pro -r -spec win32-g++ "CONFIG+=release"

好的,发布完整的源码,平台是64位Win7,4 GB DDR2-800,Core2Duo @ 3Ghz

#include <QTextStream>
#include <QVector>
#include <QElapsedTimer>

template<typename T>
class Var{
public:
    Var() {}
    Var(T val) : var(val) {}

    inline T operator+(Var& other)
    {
        return var + other.value();
    }

    inline T operator-(Var& other)
    {
        return var - other.value();
    }

    inline T operator+(T& other)
    {
        return var + other;
    }

    inline T operator-(T& other)
    {
        return var - other;
    }

    inline void operator=(T& other)
    {
        var = other;
    }

    inline T& value()
    {
        return var;
    }

private:
    T var;
};

int main()
{
    QTextStream cout(stdout);
    QElapsedTimer timer;

    unsigned count = 1000000;

    QVector<double> pin1(count), pin2(count), pout(count);
    QVector<Var<double> > vin1(count), vin2(count), vout(count);

    unsigned t1, t2, pAcc = 0, vAcc = 0, repeat = 10, pcount = 0, vcount = 0, ecount = 0;
    for (int cc = 0; cc < 5; ++cc)
    {
        for (unsigned c = 0; c < repeat; ++c)
        {
            timer.restart();
            for (unsigned i = 0; i < count; ++i)
            {
                pin1[i] = i;
                pin2[i] = -i;
                pout[i] = (i % 2) ? pin1[i] + pin2[i] : pin2[i] - pin1[i];
            }
            t1 = timer.nsecsElapsed();
            cout << t1 << endl;

            timer.restart();
            for (unsigned i = 0; i < count; ++i)
            {
                vin1[i] = i;
                vin2[i] = -i;
                vout[i] = (i % 2) ? vin1[i] + vin2[i] : vin2[i] - vin1[i];
            }
            t2 = timer.nsecsElapsed();
            cout << t2 << endl;;
            pAcc += t1;
            vAcc += t2;
        }

        pAcc /= repeat;
        vAcc /= repeat;
        if (pAcc < vAcc) {
            cout << "primitive was faster" << endl;
            pcount++;
        }
        else if (pAcc > vAcc) {
            cout << "var was faster" << endl;
            vcount++;
        }
        else {
            cout << "amazingly, both are equally fast" << endl;
            ecount++;
        }

        cout << "Average for primitive type is " << pAcc << ", average for Var is " << vAcc << endl;

    }
    cout << "int was faster " << pcount << " times, var was faster " << vcount << " times, equal " << ecount << " times, " << pcount + vcount + ecount << " times ran total" << endl;
}

相对而言,使用浮点数,Var类比浮点数快6-7%,整数约为3%。

我还运行了测试,矢量长度为10 000 000而不是原来的1000,结果仍然一致,并且有利于班级。

2 个答案:

答案 0 :(得分:2)

QVector替换为std::vector,在-O2优化级别,GCC为这两种类型生成的代码完全相同,是教学说明。

没有替换,生成的代码是不同的,但这并不奇怪,考虑到QtVector对于原始类型和非原始类型的实现方式不同(在QTypeInfo<T>::isComplex中查找qvector.h

更新看起来isComplex不会影响linner oop,即测量部分。两种类型的循环代码仍然不同,尽管非常轻微。看起来差异是由于GCC。

答案 1 :(得分:0)

我对QVector和float *的运行时间和内存分配进行了基准测试,两者之间差别很小