gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 21:48:21 27 4
gpt4 key购买 nike

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

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

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 类在大多数时候都更快,使用 int 类时平均循环时间减少了大约 5700 纳秒。在 3000 次运行中,int 比 Var 快 11 倍,后者快 2989 倍。 float 的结果类似,其中 Var 比 floatin 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;
}

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

我还用 10 000 000 的 vector 长度而不是原来的 1000 运行了测试,结果仍然一致并且有利于类(class)。

最佳答案

QVector替换为 std::vector , 在 -O2优化级别,GCC 为这两种类型生成的代码完全相同,指令对指令。

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

更新 好像isComplex不影响 linner oop,即被测部分。这两种类型的循环代码仍然不同,尽管差别很小。看起来差异是由 GCC 造成的。

关于c++ - 原始类型比 C++ 中的用户类型慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10914367/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com