×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-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

*Tek-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

Why is Fortran slower than Octave?

Why is Fortran slower than Octave?

Why is Fortran slower than Octave?

(OP)
Normally, Fortran is leaps and bounds faster than Octave. However, I've noticed that when performing similar matrix manipulations with Fortran's "spread" function, compared to Octave's "repmat" function, Octave runs about twice as fast as my compiled fortran version of the program. Is anyone able to give an explanation as to why that is? Is there something that I need to be doing in order to increase Fortran's performance?

First, here's my simple fortran program:

CODE --> fortran

program block
    double precision, parameter, dimension(1000,500) :: A = reshape([ ... ],[1000,500])
    double precision, dimension(:,:,:), allocatable :: blockL
    integer, dimension(2) :: Adim

    Adim = shape(A)
    blockL = spread(A,3,Adim(1))==spread(transpose(A),1,Adim(1))

end program block 

Now here's my corresponding program, written in Octave:

CODE --> Octave

A = [ ... ];  % This is the same "A" that was used in Fortran
Adim1 = size(A,1);
blockL = repmat(A,[1 1 Adim1])==repmat(permute(A,[3 2 1]),[Adim1 1 1]); 

Once compiled, the Fortran program takes about fifteen seconds to run. The Octave program takes about eight. Shouldn't a compiled program always be faster than an interpreted one? Any ideas on what I may be doing wrong, or how I could speed up my Fortran program?

Thanks so much for your time and attention. I appreciate any guidance that anyone is able, or willing, to provide.

RE: Why is Fortran slower than Octave?

Fortran may be faster than many packages but tools like Ocatave (or Matlab) are optimized for matrix operations - hence the name matlab. The operations like repmat, permute and == may be efficient within themselves but not when use together.

Instead of a one liner, you could split up the individual operations to see which one actually took the longest.

At a guess, octave splits up the expression into primitives, optimizes it and then executes. In Fortran, unless you are using a highly optimized compiler, each function will be executed separately.

Edit: Just checked - repmat is not a standard function. The fortran equivalent is called spread. Possibly look into the implementation of repmat if it is a local function.

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 Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

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

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close