开发者

How to improve the efficiency of the standard matrix addition algorithm in c?

How would I improve t开发者_运维问答he efficiency of the standard matrix addition algorithm?

The matrix is represented by a 2D array and is added sequentially.


I am not going to read all your code. As I can see, this is the addition part

 for(i=0;i<r1;i++)
    for(j=0;j<c1;j++)
       C[i][j]=A[i][j]+B[i][j];

I don't think this can be improved complexity-wise. As for other types of microoptimizations such as doing a ++i instead of i++ or changing the order of the loops etc. - I think you shouldn't care about these until you've run a profiler which shows you that these are your performance bottlenecks. Remember, premature optimization is the root of all evil :)


The naive double for loop is pretty close to optimal for portable code, so long as you get your two for loops in the right order. You need to be accessing the memory sequentially to get best performance.

You could unroll the loops but this won't make very much difference to performance.

If you want best performance then don't write it yourself and instead use a BLAS that has been optimised for your platform.


You can try to use GPU instead of CPU for performing intensive operations. You can use AMP for this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜