Improving the performance of vectorized code possible?
Show older comments
Hello,
I rewrote a calculation script with nested loops in vectorized code, which greatly improved computation time. Now I have done several test runs of different size mxn and am a bit surprised by the results:
for m = 16 and n = 68 the improvement is about 40 ... 45%
for m = 160 and n = 680 the improvement is about 45 ... 55%
and for m = 1600 and n = 6800 the improvement is "only" about 30%.
I would have expected that the improvement by vectorization with small problems is rather small and increases with increasing problem size or at least remains at a certain level. This behavior is unclear to me and I would like to understand it to maybe make it better.
The calculation itself uses 8 matrices / vectors that are traversed and linked in different ways. These are the basic arithmetic and sin / cos functions. In addition, the quantities m and n play a role as well as the matrices generated by ndgrid. The result is a vector of size m x 1. To achieve the result, the Matlab functions diag and sum are also used.
I have used the following techniques to speed up the calculation:
- I used ndgrid instead of the nested loop
- all nested loop calculations were vectorized
- previously doubly calculated terms are calculated only once and used again
- Instead of working in a script, I've written a function that executes the code and returns the result.
My question on this topic is divided into two parts - a more theoretical and a practical one:
1.) How is it that the improvement of the calculation speed - after it first increases - decreases again with increasing problem size? I would like to understand technically what could be or is the cause.
2.) Are there any other or additional techniques that can be used, provided that the applied techniques are implemented correctly? Which could that be? Are the Matlab functions diag and sum used for such a thing or are there better alternatives?
Thank you for your efforts and kind regards
Stephan
1 Comment
Julian Hapke
on 3 May 2018
it's hard to tell if there are better ways than
diag
and
sum
if you do not share your code.
The decreasing performance increase has propably to do with memory access, but I'm no expert there.
Accepted Answer
More Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!