简体   繁体   English

本征如何防止生成临时对象?

[英]How does Eigen prevent generating temporary objects?

In eigen documentation under the title why its interesting there is an example mentioning how eigen works. 在本征文档中,标题为何如此有趣,其中有一个示例提到本征如何工作。 The example is 这个例子是

#include<Eigen/Core>
int main()
{
  int size = 50;
  // VectorXf is a vector of floats, with dynamic size.
  Eigen::VectorXf u(size), v(size), w(size);
  u = v + w;
}

In the doc says, normally due to the design architecture of C++ the above operation would be done using a temporary. 在文档中说,通常由于C ++的设计架构,上述操作将使用临时方法完成。

VectorXf tmp = v + w;
VectorXf u = tmp;
for(int i = 0; i < size; i++) tmp[i] = v[i] + w[i];
for(int i = 0; i < size; i++) u[i] = tmp[i];

As we can see, due to the temporary object we would need one more loop. 如我们所见,由于临时对象,我们将需要一个以上的循环。 But eigen prevent generating this extra loop by not generating this temporary object. 但是本征通过不生成此临时对象来防止生成此额外的循环。 So just one loop is enough for this operation. 因此,只需一个循环即可完成此操作。

for(int i = 0; i < size; i++) u[i] = v[i] + w[i];

Although they explained how this matrix sum works but I could not understand it. 尽管他们解释了此矩阵和的工作原理,但我无法理解。 It would be nice if someone could explain it in simple words. 如果有人可以用简单的话来解释它,那就太好了。

The actual implementation of expression templates inside Eigen is quite complicated thus the description can't be "dumbed down" much. Eigen内部表达式模板的实际实现非常复杂,因此不能过多地“简化”描述。 If you are just interested in the concept of expression templates, have a look at the corresponding wikipedia article . 如果您仅对表达式模板的概念感兴趣,请查看相应的Wikipedia文章 Also read the referenced links/books if you are interested in more details. 如果您对更多详细信息感兴趣,还请阅读参考的链接/书。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM