TicToc routine - Fortran
Copy the file src/tictoc.f90
or add the package as a dependence using the Fortran Package Manager (Fpm):
[dependencies]
tictoc-fortran.git = "https://github.com/wcota/tictoc-fortran"
Import the module using use mod_tictoc
. This package provides the tictoc
object. Create one using
type(tictoc) :: ctimer
Reset the timer:
call ctimer%reset()
Use the tic
to start the clock, and toc
to pause it:
call ctimer%tic()
! do something
call ctimer%toc()
! do anything else
call ctimer%tic()
! do something
call ctimer%toc()
To see how much time was spent doing something, use the real
variable t_tot
:
write(*,*) ctimer%t_tot
A possible usage would be to stop the simulation after x
samples if the program is taking too long:
call ctimer%reset()
call ctimer%tic()
sampling: do sample=1,1000
if (ctimer%now() > 5.0_dp * 60.0_dp) exit sampling
! dynamics
enddo sampling
An example is available at example/example.f90
. To run it with Fpm, use fpm run --example
. Expected output:
Project is up to date
CPU TIME =
0.35635800000000001
Now measuring inside the loop...
CPU TIME =
0.57127699999999348
Tested with gfortran
, ifort
and ifx
compilers.