
!+
! Timer routines
!-
      module timer
      contains

      !+
      ! elapsed_time - Returns elapsed time in seconds since previous 
      !                invocation; returns 0.0 the first time it is called.
      !-
      real function elapsed_time()
      integer :: clock_count, clock_rate, clock_max
      integer, save :: last_count
      logical, save :: system_clock_not_yet_called = .true.

      call system_clock(count=clock_count,                              &
     &                  count_rate=clock_rate,                          &
     &                  count_max=clock_max)
      if (system_clock_not_yet_called) then
         system_clock_not_yet_called = .false.
         elapsed_time = 0.0
      else if (clock_count > last_count) then
         elapsed_time = real(clock_count-last_count)/real(clock_rate)
      else
         elapsed_time = real(clock_max-last_count+clock_count)          &
     &             /real(clock_rate)
      end if
      last_count = clock_count
      return
      end function elapsed_time

      end module timer

