-6

I have two very large matrices. The problem is that adding them using for loops is taking time. I came to know that we can add matrices using operator overloading in C++. Will doing so reduce the execution time?

11
  • 2
    It is too bad this isn't "StatementOverflow" Could you please reword your statement into a question? stackoverflow.com/questions/how-to-ask
    – CoderDake
    Oct 23, 2013 at 13:37
  • Adding them using bitwise operators or lookup tables or quantum computers takes time, too. First and foremost: Ask yourself about the benefit of whatever you do; if it gains you 1% better execution time, but 30% less readable code, don't. Look at other hotspots first. If it costs you 1% execution time, but yields 20% better readable code, think about it. Always ponder benefit vs. cost, and don't guess, but use facts, or just try it and measure (trying costs you a bit, though). /philosophical Oct 23, 2013 at 13:38
  • @phresnel: you have not done much performance critical linear algebra like 3D rendering, have you? These can be pretty expensive operations... It is generally speaking, an up-front design principle in certain projects. It does not need measurement time spent to get proof for the obvious. Oct 23, 2013 at 13:51
  • @LaszloPapp: Oh, it happens that I have: A, B, C, D. However, I think you missed the point of my post. You write It is generally speaking, an up-front design principle in certain projects: But what is that up-front design principle you claim I miss? Proof for what obvious? Or am I just the wrong addressee of your comment? Oct 23, 2013 at 14:09
  • @phresnel: Well, the context is about performance optimization... There are guidelines for certain projects, especially 3D what to do and what not. One does not need to get proof for the same performance problem for the 1000th time. That would be a so total time waste. ;-) Oct 23, 2013 at 14:15

4 Answers 4

5

Moving the loops into an overloaded operator will make no difference.

One way to improve performance is by using a specialized library for this, such as BLAS. A quality BLAS implementation (for example, Intel's MKL) will be much faster than anything you are likely to hand-code.

For some pointers regarding C++ wrappers for BLAS, see LAPACK wrappers for C/C++ (the question is about Windows, but the answers are broader than this).

2
  • Do you know how that performs compared to e.g. eigen? Oct 23, 2013 at 13:54
  • @LaszloPapp: No experience with eigen, but it looks like something the OP should check out.
    – NPE
    Oct 23, 2013 at 14:01
1

Operator is treated just as every other function in C++, so simply changing your adding function to an operator without changing the logic won't help.

You'd probably need to make use of some sort of SIMD calculations.

0

One way to do this is use the vector operators available at the x86 extension. Check this for an example http://en.wikipedia.org/wiki/Streaming_SIMD_Extensions#Example

If you use gnu or visual studio, they might have builtin intrinsics that you can call as a function instead of coding in assembly.

0

Put your matrices to valarray or use specialized library for that, eigen for instance. Blas is ugly and if you do not have access to commercial implementations not particularly well performing any more.

Not the answer you're looking for? Browse other questions tagged or ask your own question.