加快g ++编译时间

时间:2018-10-24 03:11:37

标签: c++ makefile build

我正在尝试编辑这种可憎的东西:

std::array< std::array< std::array< std::array< double, 4>, 4>, 4>, 4> jj()
{
  return (std::array<std::array<std::array<std::array<double, 4>, 4>, 4>, 4>){(std::array<std::array<std::array<double, 4>, 4>, 4>){(std::array<std::array<double, 4>, 4>){(std::array<double, 4>){-9.028122190419376e7, 4.651640826315136e8, -4.5396918834921384e8, -8.033215172165596e7}, (std::array<double, 4>){8.300191087589169e7, 3.4601633839642763e8, 9.00272510228725e8, -2.010030669757346e8}, (std::array<double, 4>){9.997103955698884e8, 6.731149624653375e8, -5.2484747980345505e8, -4.641782983000848e8}, (std::array<double, 4>){3.5137040497171164e8, 1.7327776817918754e8, -9.5984440374474e8, -9.23185142239465e8}}, (std::array<std::array<double, 4>, 4>){(std::array<double, 4>){-8.536925995521971e8, -5.059741076973152e8, -9.894283770114702e8, -8.121220606576325e8}, (std::array<double, 4>){8.076684436722369e8, -1.675290173958919e8, 9.731599570092971e8, -8.590807317054032e8}, (std::array<double, 4>){1.7932634059887028e8, -1.3093861992587066e8, 8.141602265653877e8, 8.876202799120474e8}, (std::array<double, 4>){-6.644718018600955e8, -2.8605410411222017e8, -9.874124825666589e8, -3.2589892924321866e8}}, (std::array<std::array<double, 4>, 4>){(std::array<double, 4>){5.379675594597301e8, 8.884497533441129e8, 5.501450148640597e8, -5.1106659445873535e8}, (std::array<double, 4>){-2.988767519658042e8, -4.113192861713449e8, 1.6137806049627256e8, 7.699661113831651e8}, (std::array<double, 4>){3.1582421267487526e8, 2.2674558366861486e8, -8.149668844275277e8, 4.560965142103479e8}, (std::array<double, 4>){1.9452519244134855e8, 1.6684188246208668e8, -4.8580637597216225e8, -7.783458071117632e8}}, (std::array<std::array<double, 4>, 4>){(std::array<double, 4>){-6.850031887398313e8, 2.9900183266052437e8, -4.3196983919609225e8, -1.7735873587772202e8}, (std::array<double, 4>){-2.4769815718822038e8, 4.334242239596813e8, 7.451272545663135e8, -5.1952566327195895e8}, (std::array<double, 4>){-8.523621443419471e8, 1.383155971939969e8, 6.514298764382088e8, -6.860752362975175e8}, (std::array<double, 4>){-1.1600626776680255e8, -7.653362576494794e8, 2.4349132048612332e8, 5.597252017095964e8}}}, (std::array<std::array<std::array<double, 4>, 4>, 4>){(std::array<std::array<double, 4>, 4>){(std::array<double, 4>){3.673178341678035e7, -4.7695321741431564e8, -6.321483840016335e8, 3.5689115088428473e8}, (std::array<double, 4>){-6.463689458045516e8, -1.9920652104829454e8, -6.91384662139636e8, -3.199100681171218e8}, (std::array<double, 4>){2.3849710015806103e8, -3.856888452865362e7, 6.234034471939282e8, -9.783953225193756e8}, (std::array<double, 4>){-7.713567298741448e8, 4.4027143721286654e8, -9.262031970194187e8, 8.313090529803119e8}}, (std::array<std::array<double, 4>, 4>){(std::array<double, 4>){-2.386853140990466e8, 7.397828224200943e8, 1.752466229437089e8, -2.0927626393041182e8}, (std::array<double, 4>){-8.895319563767164e8, 8.058350349255462e8, 5.59493599965055e7, -8.014117629671816e8}, (std::array<double, 4>){9.525092865394981e8, -2.1440826373036098e8, 4.25812414990675e8, -8.120293239346253e8}, (std::array<double, 4>){-6.41034558763449e8, -5.6004621158952475e7, -9.704411962819092e8, -9.048249507695416e8}}, (std::array<std::array<double, 4>, 4>){(std::array<double, 4>){1.7367634734240103e8, -4.239224915715319e8, 6.970505579493117e8, -9.083843900667126e8}, (std::array<double, 4>){-7.365738749125159e8, -5.319167553802944e8, -9.822774238594356e8, -7.640013839048612e8}, (std::array<double, 4>){6.53279760605813e8, -6.01806177735822e8, 7.58663128377279e8, -5.257430200078712e8}, (std::array<double, 4>){8.029611355575571e8, -9.77937051350517e8, 6.607921948465097e8, -9.224887323711383e8}}, (std::array<std::array<double, 4>, 4>){(std::array<double, 4>){-8.528628338605886e8, 7.237580906413536e8, 8.647984520693469e8, -8.91201608880968e8}, (std::array<double, 4>){-9.517609222602383e8, 5.740149236714525e8, 2.3561130709515476e8, -4.7770573975081974e8}, (std::array<double, 4>){3.652898917555714e7, -3.8644825087866485e8, 5.156779638734515e8, -1.3245306752895308e8}, (std::array<double, 4>){6.339031285761676e8, -2.0403537036685646e8, 4.1950606260563016e7, 2.228985229530673e8}}}, (std::array<std::array<std::array<double, 4>, 4>, 4>){(std::array<std::array<double, 4>, 4>){(std::array<double, 4>){6.411820068794494e8, 7.732122362970955e8, -8.91891054785261e8, 8.893790477303574e8}, (std::array<double, 4>){2.847044475960748e8, 6.628136448158114e8, 1.548937860363574e8, 4.693589607751038e8}, (std::array<double, 4>){-4.2311930508194196e8, -2.9347266557125723e8, -4.965755497744915e8, 5.696978940306258e8}, (std::array<double, 4>){2.1414254063139558e8, 5.1076351602559304e8, 9.792275620973918e8, -9.880244818228383e8}}, (std::array<std::array<double, 4>, 4>){(std::array<double, 4>){6.454887542286801e8, -9.904130155738571e8, -2.0345050464252365e8, -1.457288904100957e8}, (std::array<double, 4>){-5.089872063452674e8, 4.0674983283995795e8, 7.924681334023275e8, -4.216247136499163e8}, (std::array<double, 4>){2.5021906580387807e8, 1.4414189931748867e8, 1.2014475080691671e8, 5.1083945014864683e8}, (std::array<double, 4>){-4.46986705606482e8, 8.046788064404113e8, 6.173215559411869e8, -3.6842806627833307e8}}, (std::array<std::array<double, 4>, 4>){(std::array<double, 4>){1.9017283209536362e8, 9.869944411974008e8, -9.252259232910815e8, -3.067124592256409e8}, (std::array<double, 4>){3.8202207213874364e8, 3.770434279448426e7, -8.131620926943157e8, -7.14000393127284e8}, (std::array<double, 4>){-7.1414750408233e8, 4.6640207611017966e8, 6.687317437161374e7, 2.99514879338814e8}, (std::array<double, 4>){5.2439041437292767e8, -8.924255864528936e8, -3.5041103846071756e8, -9.182860694971386e8}}, (std::array<std::array<double, 4>, 4>){(std::array<double, 4>){1.248089786262002e8, -3.225883993745688e8, 7.387869750833535e8, 9.841290599474988e8}, (std::array<double, 4>){6.519370917252836e8, 2.815321026865206e8, -6.046362245029492e8, 2.0732671653132415e8}, (std::array<double, 4>){7.57221860473655e8, -1.8509963151543212e8, 2.898069006624291e8, 8.24667301484312e8}, (std::array<double, 4>){-9.52059607736028e8, -6.079398285124476e8, 6.7194670699821e7, 2.5021734394579887e8}}}, (std::array<std::array<std::array<double, 4>, 4>, 4>){(std::array<std::array<double, 4>, 4>){(std::array<double, 4>){1.688935275052848e8, 6.332500220759096e8, 9.946370748845999e8, -1.6604638259154475e8}, (std::array<double, 4>){-8.06724318795696e8, 8.508509176875343e8, 7.531446789137385e8, 4.831904721169164e8}, (std::array<double, 4>){-4.046918114607099e8, -6.56844785141228e8, -4.837609082116079e8, -5.864021524356784e8}, (std::array<double, 4>){-7.431266945809747e8, -3.5321703848676276e8, 6.057167707756605e8, 1.6343899036836243e8}}, (std::array<std::array<double, 4>, 4>){(std::array<double, 4>){-2.1571453379304934e8, 9.321116821399493e8, -4.431797119895941e8, -3.0886833667083466e8}, (std::array<double, 4>){-6.039001749226713e8, 1.461438442718861e8, -7.831607581445485e8, 2.186955494646406e7}, (std::array<double, 4>){1.608074886798873e8, -9.066633321283468e8, 8.247092542628763e8, 8.718939616142144e8}, (std::array<double, 4>){9.665761836378465e8, -3.645832367475915e7, -913222.2242269516, 2.6149274749155402e7}}, (std::array<std::array<double, 4>, 4>){(std::array<double, 4>){2.2101851529819584e8, -6.512151250285187e8, 5.660716394915235e7, 4.471397355309706e8}, (std::array<double, 4>){-4.017290664573574e7, -8.244366275876291e8, -6.358856871708645e8, -1.5117934622971487e8}, (std::array<double, 4>){-4.795819524595091e8, -3.63563449992679e8, -8.981004698876072e8, 7.952220350549572e8}, (std::array<double, 4>){-9.394207830564592e8, 9.350810254096992e8, -3.8558474115604174e8, -7.17048420137236e8}}, (std::array<std::array<double, 4>, 4>){(std::array<double, 4>){3.698437935648396e8, -7.590677756502736e8, -9.556009282601465e8, 7.129866840360484e8}, (std::array<double, 4>){9.955320518576987e8, 6.359667658125978e8, 7.007770263072741e8, -6.608635817552066e7}, (std::array<double, 4>){-5.19551977931501e8, 7.843508758427446e8, -1.8793168989581406e8, 4.0735345720076275e8}, (std::array<double, 4>){-2.752189670036937e8, 9.591600609294622e8, -8.209663077150682e8, -8.769230862049687e7}}}};
}

这是计算机生成的代码,我将经常生成此代码的变体(即,常数是变化的,而暗数是不变的)。我希望为调用jj()的用户提供超快速的构建过程和执行速度(即,我想同时优化运行时间和编译时间)。 jj()函数在此示例中未执行任何操作。但是它将加载具有已知值的矩阵,并进行矩阵运算并返回结果。具有已知常数的矩阵可能非常大(1000x1000x50x50)。我有1000个不同的应用程序可以调用jj()函数的不同变体(只是常量在变化。而且这些应用程序每个都经常调用jj()。我想一种编译{的这1000个版本的方法{1}}并允许有效地多次调用这些分隔的变体。我每隔几分钟重新编译以更新这1000个变体中的每一个的常数。我需要以毫秒为单位的编译时间,以便我的应用程序可以启动进行备份早点。

这是我的编译代码:

jj()

0.5s的编译时间太慢,我可能在一分钟内这样做很多次。 该代码正在初始化256个值。我可以拥有更多。我正在考虑将这些值存储到文件中并加载它们。但这会使运行时间变慢,因为您是从磁盘读取的。

我希望编译和运行时间都尽可能快。我有什么选择?

1 个答案:

答案 0 :(得分:1)

您应该重写生成器。在堆栈上传递大对象(1000x1000x50x50)不是一个好主意。最好定义一个静态常量。另外,初始化可以更短,不需要所有这些强制转换:

inline constexpr std::array< std::array< double, 4>, 4> const jj_val
{
    0.0, 0.0, 0.0, 0.0
,   0.0, 0.0, 0.0, 0.0
};

inline constexpr auto & jj(void) { return jj_val; }

或者只是从常规文件加载数据。请注意,从文件读取不会使运行时间明显变慢,因为即使将数据嵌入可执行文件中,仍会从磁盘读取该文件。