Making FORTRAN code more efficient

Hi, I have a very large, very old FORTRAN code that I work with. The code is quite messy and I was wondering if I can speed up execution time by finding subroutines that code execution spends the most time in. Is there any kind of software I can use to see where the code spends most of the execution process?

Also, I made some changes to the code so it writes a whole bunch of extra output files during execution. I just ran the unmodified version of the code and noticed it seems to run a lot faster than my modified version. Does writing to text files bog down the execution that much?

Hi.

Here is a contrived code in Fortran-77 style (change the do-enddo to do ... statement-number, if you wish):

	program main
	parameter ( n = 10 000 000 )
	dimension t(n)
	do i = 1, n
	t(i) = i
	enddo
	call a ( t, int( .10*n ), sum )
	call b ( t, int( .25*n ), sum )
	call c ( t, n, sum )
	end
	subroutine a ( t, n, sum )
	write(*,*) " Entered: a"
	call a1 ( t, n, sum )
	return
	end
	subroutine a1 ( t, n, sum ) 
	dimension t(n)
	write(*,*) " Entered: a1"
	sum = 0
	do i=1,n
	sum = sum + t(i)
	enddo
	return
	end
	subroutine b ( t, n, sum )
	dimension t(n)
	write(*,*) " Entered: b"
	sum = 0
	do i=1,n
	sum = sum + t(i)
	enddo
	call b1
	return
	end
	subroutine b1
	write(*,*) " Entered: b1"
	call b2
	return
	end
	subroutine b2
	write(*,*) " Entered: b2"
	return
	end
	subroutine c ( t, n, sum )
	dimension t(n)
	write(*,*) " Entered: c"
	sum = 0
	do i=1,n
	sum = sum + t(i)
	enddo
	return
	end

We compile with GNU Fortran, and state that we wish to include profiling code:

#!/usr/bin/env bash

# @(#) s1	Demonstrate obtaining a Fortran execution profile.

pe() { for i;do printf "%s" "$i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
pe
# (Local utility version)
version gfortran gprof

FILE=one.f
rm -f a.out gmon.out

gfortran -pg $FILE
./a.out

gprof --brief

exit 0

Producing (excerpt):

gfortran GNU Fortran (Debian 4.3.2-1.1) 4.3.2
GNU gprof (GNU Binutils for Debian) 2.18.0.20080103
  Entered: a
  Entered: a1
  Entered: b
  Entered: b1
  Entered: b2
  Entered: c

...

  %   cumulative   self              self     total           
 time   seconds   seconds    calls  ms/call  ms/call  name    
 45.63      0.10     0.10        1   100.38   100.38  c_
 41.06      0.19     0.09        1    90.34   220.83  MAIN__
  9.13      0.21     0.02        1    20.08    20.08  b_
  4.56      0.22     0.01        1    10.04    10.04  a1_
  0.00      0.22     0.00        1     0.00    10.04  a_
  0.00      0.22     0.00        1     0.00     0.00  b1_
  0.00      0.22     0.00        1     0.00     0.00  b2_

Which would suggest that c and MAIN be looked at.

See man pages for details. Good luck ... cheers, drl