!                                                     B Bunk 5/2014
!                                                     rev
!   cpudif()  returns the cpu-time (in sec) elapsed since the
!       previous call (for the very first call, cpudif()=0. )
!   clkdif()  returns the wallclock time (in sec) elapsed since the
!       previous call (for the very first call, clkdif()=0. )

module timedif

contains

  real function cpudif()
    logical, save :: first = .true.
    real, save :: told, tnew

    call cpu_time(tnew)    ! Fortran 95

    if (first) then
       told = tnew
       first = .false.
    endif
    cpudif = tnew - told
    told = tnew
  end function cpudif

  real function clkdif()
    logical, save :: first = .true.
    integer(8), save :: cntnew, cntold, count_rate

    call system_clock(cntnew, count_rate)    ! Fortran 90

    if (first) then
       cntold = cntnew
       first = .false.
    endif
    clkdif = real(cntnew - cntold)/count_rate
    cntold = cntnew
  end function clkdif

end module timedif
