小编dar*_*uki的帖子

使用空构造函数会使数组未初始化,从而导致计算速度变慢

我对一件事感到非常困惑...如果我将构造函数添加到 struct A 中,那么 for 循环中的计算会变得慢很多倍。为什么?我不知道。

在我的计算机上,输出中的代码片段的时间为:

有构造函数:1351

没有构造函数:220

这是一个代码:

#include <iostream>
#include <chrono>
#include <cmath>

using namespace std;
using namespace std::chrono;

const int SIZE = 1024 * 1024 * 32;

using type = int;

struct A {
    type a1[SIZE];
    type a2[SIZE];
    type a3[SIZE];
    type a4[SIZE];
    type a5[SIZE];
    type a6[SIZE];

    A() {} // comment this line and iteration will be twice faster
};

int main() {
    A* a = new A();
    int r;
    high_resolution_clock::time_point t1 = high_resolution_clock::now();
    for (int i …
Run Code Online (Sandbox Code Playgroud)

c++ optimization default-constructor compiler-optimization visual-studio-2013

6
推荐指数
1
解决办法
273
查看次数