Matlab vs. Fortran
Matlab vs. Fortran
(OP)
I recently went through the rigor of converting an old Fortran code (77, 90?) to a matlab script.
The program is supposed to analyze 45,000 data points, using a couple nested while loops. (FORTRAN code uses several nasty GOTO statements to jump backwards and forwards.)
Compiled Fortran program (with GOTO statements) running in MS-DOS window with limited RAM runs through all points in ~2 seconds.
The Matlab script (using while loops) running on brand new desktop with full RAM takes about 3 minutes.
I've heard of for loops in Matlab be described as bottle necks - but is that what is happening here?
The program is supposed to analyze 45,000 data points, using a couple nested while loops. (FORTRAN code uses several nasty GOTO statements to jump backwards and forwards.)
Compiled Fortran program (with GOTO statements) running in MS-DOS window with limited RAM runs through all points in ~2 seconds.
The Matlab script (using while loops) running on brand new desktop with full RAM takes about 3 minutes.
I've heard of for loops in Matlab be described as bottle necks - but is that what is happening here?





RE: Matlab vs. Fortran
From my own experience, I can generally get vectorized Matlab code to run somewhere between 1.5-2 times as slow as the same algorithm coded into a MEX file. Your factor of over over 100 rings alarm bells.
RE: Matlab vs. Fortran
I fixed it by initializing my array as zeros(size) to the fullest size it would see during execution.
RE: Matlab vs. Fortran
=====================================
Eng-tips forums: The best place on the web for engineering discussions.
RE: Matlab vs. Fortran
startloop
shuffle all variables along by one word
increment size of array
execute the bit you thought you were doing
rinse and repeat
instead of
set up all variables
startloop
execute the bit you thought you were doing
rinse and repeat
The other option would be to store arrays as non contiguous variables, but I'd guess the authors of /MAT/LAB thought that was likely to be counterproductive.
Cheers
Greg Locock
Please see FAQ731-376 for tips on how to make the best use of Eng-Tips.
RE: Matlab vs. Fortran
a(1000,1000)=0;
Will create a 1000 by 1000 array.
One common trick is to always loop backwards through an array so that it's all allocated on the first iteration:
% Slow
for m=1:1000
for n=1:1000
x(m,n)=0;
end
end
% fast
for m=1000:-1:1
for n=1000:-1:1
x(m,n)=0;
end
end
RE: Matlab vs. Fortran
=====================================
Eng-tips forums: The best place on the web for engineering discussions.