필터 지우기
필터 지우기

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

조회 수: 23 (최근 30일)
Jason
Jason 2016년 10월 12일
댓글: Adam 2016년 10월 12일
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
Adam
Adam 2016년 10월 12일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2016년 10월 12일
편집: Jan 2016년 10월 12일
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
Jason
Jason 2016년 10월 12일
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 2016년 10월 12일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB Compiler에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by