subroutine measure_time(print_total, log_unit)
use iso_fortran_env, only: int64, real64
implicit none
integer, intent(in) :: print_total, log_unit
integer(int64), save :: start_clock, previous_clock
integer(int64) :: clock_rate, current_clock
real(real64), save :: start_cpu_time, previous_cpu_time
real(real64) :: current_cpu_time, elapsed_cpu_time, elapsed_wall_time
logical, save :: first_call = .true.
if (first_call) then
call system_clock(start_clock, clock_rate)
call cpu_time(start_cpu_time)
previous_clock = start_clock
previous_cpu_time = start_cpu_time
first_call = .false.
end if
call system_clock(current_clock, clock_rate)
call cpu_time(current_cpu_time)
elapsed_wall_time = real(current_clock - previous_clock, &
real64) / real(clock_rate, real64)
elapsed_cpu_time = current_cpu_time - previous_cpu_time
write(log_unit, "(3X, A, F10.3, 3X, A, F10.3)") &
"Step CPU time (seconds): ", elapsed_cpu_time, &
"Wall time (seconds): ", elapsed_wall_time
previous_clock = current_clock
previous_cpu_time = current_cpu_time
if (print_total /= 0) then
elapsed_wall_time = real(current_clock-start_clock, &
real64) / real(clock_rate, real64)
elapsed_cpu_time = current_cpu_time - start_cpu_time
write(log_unit, "(3X, A, F10.3, 3X, A, F10.3)") &
"Total CPU time (seconds): ", elapsed_cpu_time, &
"Wall time (seconds): ", elapsed_wall_time
end if
end subroutine measure_time