Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Matlab vs. Fortran

Status
Not open for further replies.

brianpaul

Mechanical
Jan 18, 2005
19
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?
 
Replies continue below

Recommended for you

If you have simply duplicated your algorithm in Matlab, you are bound to see a huge slowdown. The next trick is to vectorize it. But it'll still be slower than FORTRAN (if your FORTRAN is well written).

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.
 
I ran Matlab's profile routine, which led me to the solution (a solution which I already thought I tried, but on third look wasn't implemented correctly...)

I fixed it by initializing my array as zeros(size) to the fullest size it would see during execution.
 
out of curiosity, does anyone know why initializing variables affects the program speed so dramatically?

=====================================
Eng-tips forums: The best place on the web for engineering discussions.
 
Because otherwise the following loop gets executed

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.
 
It's not so much the initialising that speeds things up, it's the preallocation of the array. Note that you don't even need to set the whole array to anything specific, just the last member. e.g.

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

 
Thanks Greg and SG. That makes sense.

=====================================
Eng-tips forums: The best place on the web for engineering discussions.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor