Expression templates: improving performance in evaluating expressions?

后端 未结 2 2014
天涯浪人
天涯浪人 2021-01-13 03:17

By the expression templates technique, a matrix expression like

D = A*B+sin(C)+3.;

is pretty much equivalent, in terms of computing perform

2条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-13 04:00

    In c++11 you can use auto

    auto D = A*B+sin(C)+3.;
    

    assuming you are using expression templates, the type of D would be . Now, you have to use this carefully, because you are saving up some memory (no need to allocate space for a matrix) but depending on how you use it this may not be the best.

    Think about

    F = D*E

    An element D[i][j] needs to be "visited" many times when computing D*E (actually n times where n is the size of the matrices). If D is a plain-matrix type this is no problem. If D is an expression you are evaluating it many, many times.

    On the contray, doing

    F = D + E
    

    is fine.

    Think about this: you can not write F = E*(A*B+sin(C)+3.); using only two nested loops.

提交回复
热议问题