How do I make a MATLAB calculation faster?

조회 수: 24 (최근 30일)
Modestas Sekreckis
Modestas Sekreckis 2011년 5월 22일
답변: Mohamed ALIMOUSSA 2020년 4월 4일
[EDIT: Sun May 22 16:17:00 UTC 2011 - clarify - MKF]
Hi, I'm working on a 3D trajectory searches and search path is sometimes very slow. Maybe it is somehow possible to speed up the MATLAB program? Maybe I can put more CPU resources to MATLAB?

답변 (4개)

Jan
Jan 2011년 5월 23일
There are three methods to speedup a MATLAB calculation:
  1. Faster hardware - especially more RAM helps for large data.
  2. Improved code: I haven't seen a MATLAB function, which cannot be accelerated by an experienced programmer. Trivial things can matter, like changing the matrix access from rowwise to columnwise order. Even one-liners from the MATLAB toolbox can be improved (see BLANKS and NOW). Therefore it is always worth to try posting the code here.
  3. Slowdown the time of the user by applying relativistic effects: While you are travelling to Jupiter and back to earth with nearly lightspeed, the computer on the earth can perform much more operations measured in your personal time.
There are no other methods. This forum is a very good location to work on point 2.
There is a point 3b also, but this only applies for human: An operation taking more than 5 seconds without giving feedback causes stress. Therefore progressbars are used, although they actually waste time. Leaving the computer during long operations to drink a cup of coffee can help also.
Do you really expect that there is a secret flag like "matlab -doublespeed", which accelerates MATLAB magically?! If so, I admire your positive estimation in the power of the Mathworks programmers. But the world is even better: Mathworks has enabled the double-speed already since MATLAB 6.5! It's called JIT. Simply disable it (temporarily!) and measure the time of your program again: "feature('accel', 'off'), feature('jit', 'off')".
  댓글 수: 2
Daniel Shub
Daniel Shub 2011년 5월 23일
In r2010b blanks is not a one-liner (there are two lines).
Jan
Jan 2011년 5월 23일
@Daniel: Correct. But it *should* be "b(1:n) = ' ';". For BLANKS(1) the difference is just some nano-seconds, but for BLANKS(1000) the one-liner is 3.3 times faster (Matlab 2009a, single thread).
Of course, BLANKS(1000) is a strange task and the absolute timings of about 1E-4 seconds suggest, that BLANKS will most likely never be a bottleneck. But especially BLANKS is an example for a trivial function, which can be improved easily. See also: FILESEP,PATHSEP,ISPC,ISUNIX (it is known at install time, if the toolbox runs under Windows or Linux, afaics), IND2RGB, RMFIELD, ISCELLSTR, STRCAT, STRVCAT, ... See also James Tursa's TYPECASTC, which simply rocks.

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


John D'Errico
John D'Errico 2011년 5월 22일
It is impossible to answer your question without more information. Very often slow code is the fault of the person doing the writing. Have you used good practices there? How can I know? In fact, the very fact that you have asked this question makes me expect that this is the issue.
So you need to use the profile tool, looking for bottlenecks in your code. Where does it take most of the time? Then work on improving those pieces of code.
  댓글 수: 4
John D'Errico
John D'Errico 2011년 5월 22일
Note that I have several times made serious improvements in the efficiency of an old code when I looked at a sub-problem in a different way, or by just looking for an optimization where I had not before.
Use the profiler and you may be surprised. Or make your problem smaller. Or accept the fact that big problems take time to solve.
Jan
Jan 2011년 5월 23일
@John: I'm convinced, that experienced programmers as you can accelerate *any* existing function. The question is only, if it is worth to try it: Spending hours to make a program using milliseconds less time is not an efficient method to solve a problem.

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


Daniel Shub
Daniel Shub 2011년 5월 22일
If you are not willing to change your code, and presumably not willing to change your hardware, then there is little you can do to speed up MATLAB. Launching MATLAB without the desktop and without java will free up some resources (of course your code may require java or the desktop to run). You could also launch MATLAB with a higher priority. Finally, you could reinstall your OS with minimal services (no network, no virus scan, no search indexing, etc) and applications to tweak out a few more resources. The odds of any of these things making a difference is small, even if you know where the bottleneck is.
If you are willing to examine your code and consider optimizing it, you can get huge benefits. Trivial things like preallocation can make big differences. More complicated things like vectorization and parallelization can also have big impacts.

Mohamed ALIMOUSSA
Mohamed ALIMOUSSA 2020년 4월 4일
You can lunch multiple Matlab instances and divide your code by these instances. Since as you said, a matlab instance doesn't talk all the CPU utilisation.

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by