×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Matlab vs. Fortran

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?

RE: Matlab vs. Fortran

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.

RE: Matlab vs. Fortran

(OP)
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.

RE: Matlab vs. Fortran

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.

RE: Matlab vs. Fortran

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.

RE: Matlab vs. Fortran

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

RE: Matlab vs. Fortran

Thanks Greg and SG. That makes sense.

=====================================
Eng-tips forums: The best place on the web for engineering discussions.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources