Time measurement functions in Fortran


CPU time in Fortran: ''CPU_TIME''

CPU_TIME is a Fortran 90 intrinsic subroutine. To get the elapsed time, you must call CPU_TIME twice and subtract the starting time from the ending time value. Usage example:

example
  implicit none
  integer               :: j, n
  real,dimension(10000) :: x
  real                  :: t1,t2
  . . . .
  call CPU_TIME( t1 )
  !do j=1,10000
  !  x(j)=log(23.)*j
  !end do
  x(:) = log(23.)*[ (j,j=1,10000) ]
  call CPU_TIME( t2 )
  . . . .
  read *, n ; print *, x(n)
  print *,t2 - t1
  . . . .

Comments

  • The t1 and t2 types are REAL in Fortran; the CPU_TIME is measured in seconds.
  • If the returned argument is negative, it means that the functionality is not available.

Clock time in Fortran : ''SYSTEM_CLOCK''

SYSTEM_CLOCK is a Fortran 90 intrinsic subroutine. To get the elapsed time, you must call SYSTEM_CLOCK twice and subtract the starting time from the ending time value. Usage example:

example
  . . .
  INTEGER :: &
     nb_ticks_initial & ! initial value of the clock tick counter
     nb_ticks_final   & ! final value of the clock tick counter
     nb_ticks_max     & ! maximum value of the clock counter
     nb_ticks_sec     & ! number of clock ticks per second
     nb_ticks           ! number of clock ticks of the code
  REAL :: elapsed_time  ! real time in seconds
 
  ! Initialisations
 
  CALL SYSTEM_CLOCK(COUNT_RATE=nb_ticks_sec, COUNT_MAX=nb_ticks_max)
  . . .
  CALL SYSTEM_CLOCK(COUNT=nb_ticks_initial)
  . . .
  !      <<<<<<<<<<<<< the part of the code to be evaluated >>>>>>>>>>>>>
  . . .
  CALL SYSTEM_CLOCK(COUNT=nb_ticks_final)
  nb_ticks = nb_ticks_final - nb_ticks_initial
  IF (nb_ticks_final < nb_ticks_initial) &
          nb_ticks = nb_ticks + nb_ticks_max
  elapsed_time   = REAL(nb_ticks) / nb_ticks_sec
  . . .