Template-ing a 'for' loop in C++?

后端 未结 10 646
没有蜡笔的小新
没有蜡笔的小新 2020-12-25 12:58

I have a C++ snippet below with a run-time for loop,

for(int i = 0; i < I; i++)
  for (int j = 0; j < J; j++)
    A( row(i,j), column(i,j)         


        
10条回答
  •  南方客
    南方客 (楼主)
    2020-12-25 13:20

    I'm not a fan of template meta-programming, so you may want to take this answer with a pinch of salt. But, before I invested any time on this problem, I'd ask myself the following:

    1. Is my for loop a bottleneck?
    2. Is unrolling it going to make any difference? Easily tested by hand-unrolling and measuring.

    In many compilers/cpus, the "looped" version can give better performance due to cache effects.

    Remember: Measure first, optimise later - if at all.

提交回复
热议问题