Clear Filters
Clear Filters

how many times c++/c can faster than Matlab

23 views (last 30 days)
Jason
Jason on 12 Oct 2016
Commented: Adam on 12 Oct 2016
Hello everyone,
I am writing some codes to compute a large problem. Due to there are many 'for' loops, and some matrix are six dimensional, use matlab is not efficiency. the program didn't stop in 2 weeks in Matlab. I have seen something through MEX function, but someone say MEX is often more trouble than it is worth. Maybe I should write in C/C++. Do you have any suggestions in this topic? Thanks a lot !
  1 Comment
Adam
Adam on 12 Oct 2016
It is impossible to say how much faster code would be in C++ than Matlab. It is almost always possible to speedup Matlab code itself in numerous ways. Also it is equally possible to write inefficient C++ code as it is to write inefficient Matlab code.
Some things will certainly be faster in C++ (though these can be called through Mex - it doesn't have to be entirely C++), but it isn't the first port of call for speeding up code unless maybe you ultimately want the code in C++ anyway.
I bought Yair Altman's book on accelerating Matlab performance a while back. I haven't read much of it yet as I haven't really been needing to speed up my code beyond the techniques I am already aware of, but there are 700+ pages of it, only part of which is about Mex so there are very many ways to speed up code in Matlab first if you want to try to.

Sign in to comment.

Answers (1)

Jan
Jan on 12 Oct 2016
Edited: Jan on 12 Oct 2016
Clean up your code at first:
  • preallocate all outputs (!!!!!!)
  • avoid repeated computations
  • process the data in columnwise order
  • try vectorize the code - but if large temporary arrays are created, this can slow down the computations potentially
Then use the profiler to find the bottleneck. It is not worth to mex the code, if an exp() is the most expensive operation, because this will take the same time when called from C.
If you post your code, useful suggestions are possible and likely.
Sometimes Matlab functions can be accelerated by a factor of 100 when they are written as an efficient Mex code, e.g. the datestr function family.
  2 Comments
Jason
Jason on 12 Oct 2016
Hi, Jan, thank you for your response! I add my code in the attached file, could you please see it,I don't know how to optimize it for the moment. Thank you very much !
Adam
Adam on 12 Oct 2016
The first (or one of the first) thing you should be doing is factoring your code out into much smaller functions. Then your main outer function becomes far tidier and profiling results can make a lot more sense to home in on the problem areas.
I don't subscribe to the excessive software engineering practices of functions being no more than 4 lines long, but they should certainly be a lot shorter than that to be easily readable, debugable, etc.
Just find portions of code that can be moved out into some other function and keep going along that route to start with. In itself this won't speed up the code at all, but it is an important step in the direction of understanding how to.

Sign in to comment.

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!