|
|
|||
![]() |
Department of Engineering |
| University of Cambridge > Engineering Department > computing help > programs > matlab |
Matlab is easy to use, but the easiest method might not be the fastest. Fortunately, some of the simpler ways to improve the speed of matlab programs are also amongst the most effective, leading to order-of-magnitude improvements.
Firstly, make sure you're not writing unnecessary code. Each new version of matlab has new functions that might be useful, and functions that have been made faster. Remember that some of matlab's commands are scripts and some are built-in functions which are going to be faster than anything you can write. Use the which command to find out whether or not a function is a script (cumsum for example, isn't).
Also make sure you're up to date with matlab's newer features: Some (like cells and structures) might make your code tidier but slower; others (like the newer visualisation routines) may speed up your code considerably.
Matlab 6.5 introduced the "JIT-Accelerator" which greatly speeds up some scripts with big simple "for" loops. If you have pre-2006 matlabs it might be worth use the profile routine to help you adapt your code to take advantage of it. Newer matlabs don't show JIT information in the profile output, but it's still worth helping the JIT-Accelerator. The JIT-Accelerator example shows how programs can run 10 times faster if tweaked. Not all of the tweaks are obvious - indeed, some of them would nearly halve the speed of the program were it not for the JIT-Accelerator.
Then go through this checklist of issues to consider
You can convert a function into matlab's internal form yourself using the pcode command, but it's hardly worth it.
function sp = spdemo(arg1, arg2)
% Comparison of sparse vs full matrices
if (nargin ~= 2)
error('Give order and density');
return
end
S = sprandn(arg1,arg1,arg2);
F = full(S);
% Compare speed.
t0=cputime;
B = S * S;
stime=cputime-t0;
t0=cputime;
C = F * F;
ftime=cputime-t0;
sprintf('Order %d matrix, density %f: Full=%f, Sparse=%f', arg1, ...
arg2, ftime, stime)
Structures of arrays are faster than arrays of structures.
Note that if you're processing a 2D array, it's faster to scan down the columns than along the rows.
profile on spdemo(100, .1) profile off profile viewerYou'll need to click on the function names displayed by the viewer in order to get detailed information. Within the department one particular diagnostic session led to a speedup of 3000 times (to several hours to several seconds) when it turned out that the same huge .mat file was being loaded on every iteration of a loop.
It's possible to combine the ease of writing M-files with the speed provided by writing in C or Fortran. Compilers (Mathworks' mcc or the free matcom) convert M-files into compiled C code. Our Signal Processing & Communications group have used mcc successfully. However, mcc isn't free. Also speed improvements depend strongly on the particular application. In some cases, performance improves by more than 200 times, while other files show little or no improvement. Note also that the compiler is rather expensive, and users will need to have a big file installed before the matlab application will work.
Whether or not you use a compiler, it's worth using other optimisation techniques too.
If 1 CPU isn't enough, you could use more. You may have a multi-CPU machine or access to several machines. Note that the current version of matlab uses 1 CPU by default, but you can change that behaviour - see our Matlab parallelisation section. You might even be able to use a graphics accelerator board to speed matlab up. Options include
| | computing help | Matlab | |