fortime Module

fortime provides the timer derived type plus the public real kind rk and integer kind ik. A single timer object can measure several kinds of elapsed time:

  • timer_start / timer_stop use the intrinsic system_clock.
  • ctimer_start / ctimer_stop use the intrinsic cpu_time.
  • dtimer_start / dtimer_stop use the intrinsic date_and_time.
  • otimer_start / otimer_stop use omp_get_wtime when USE_OMP is defined.
  • mtimer_start / mtimer_stop use MPI_Wtime when USE_MPI is defined.

The usual workflow is to call a start routine, execute the code being timed, call the matching stop routine, and then read the matching public result component. Stop routines can also print the elapsed time, average it over a positive loop count, and customize the output label, color, and real format.

Basic usage:

use fortime, only: timer
type(timer) :: t

call t%timer_start()
call work()
call t%timer_stop(print=.false.)
print *, t%elapsed_time

Loop-averaged usage:

call t%timer_start()
do i = 1, nloops
   call work()
end do
call t%timer_stop(nloops=nloops, message='Average time:', print=.true.)

Note

The system_clock, OpenMP, and MPI timers are the preferred wall-clock timers. The date_and_time timer is a civil-clock fallback and is not guaranteed to be monotonic.


Uses

  • module~~fortime~~UsesGraph module~fortime fortime iso_fortran_env iso_fortran_env module~fortime->iso_fortran_env

Used by

  • module~~fortime~~UsedByGraph module~fortime fortime program~check check program~check->module~fortime program~example1 example1 program~example1->module~fortime program~example2 example2 program~example2->module~fortime program~example3 example3 program~example3->module~fortime program~example4 example4 program~example4->module~fortime program~example5 example5 program~example5->module~fortime program~example6 example6 program~example6->module~fortime

Variables

Type Visibility Attributes Name Initial
integer, public, parameter :: ik = int64

Integer kind used by the internal tick counters and civil-date calculations. It is currently mapped to int64.

integer, public, parameter :: rk = real64

Real kind used by the public timing results and internal real-valued calculations. It is currently mapped to real64.


Derived Types

type, public ::  timer

Timing object with independent state for each supported backend.

Read more…

Components

Type Visibility Attributes Name Initial
real(kind=rk), public :: cpu_time = 0.0_rk

Most recent successful CPU-time measurement in seconds.

real(kind=rk), public :: elapsed_dtime = 0.0_rk

Most recent successful date_and_time elapsed time in seconds.

real(kind=rk), public :: elapsed_time = 0.0_rk

Most recent successful system_clock elapsed time in seconds.

integer(kind=ik), private :: clock_max = 0_ik

Cached maximum system_clock count used to handle one counter wrap.

integer(kind=ik), private :: clock_rate = 0_ik

Cached system_clock count rate in ticks per second.

integer(kind=ik), private :: clock_start = 0_ik

system_clock count captured at the start of the wall-clock interval.

real(kind=rk), private :: cpu_start = 0.0_rk

CPU time, in seconds, captured by ctimer_start.

real(kind=rk), private :: dtime_start_utc = 0.0_rk

UTC epoch seconds computed from date_and_time at dtimer_start.

logical, private :: is_cpu_started = .false.

True after ctimer_start and before a successful ctimer_stop.

logical, private :: is_dtime_started = .false.

True after dtimer_start and before a successful dtimer_stop.

logical, private :: is_paused = .false.

True after timer_pause and before a matching timer_resume.

logical, private :: is_started = .false.

True after timer_start and before a successful timer_stop.

integer(kind=ik), private :: pause_start = 0_ik

system_clock count captured when the wall-clock timer is paused.

integer(kind=ik), private :: paused_ticks = 0_ik

Total paused duration, in system_clock ticks, excluded from the result.

Type-Bound Procedures

procedure, public :: ctimer_start

Start a CPU-time measurement.

procedure, public :: ctimer_stop

Stop a CPU-time measurement and update cpu_time.

procedure, public :: ctimer_write

Append cpu_time to a text file.

procedure, public :: dtimer_start

Start a date_and_time civil-clock measurement.

procedure, public :: dtimer_stop

Stop a date_and_time civil-clock measurement and update elapsed_dtime.

procedure, public :: dtimer_write

Append elapsed_dtime to a text file.

procedure, public :: timer_pause

Pause a running system_clock wall-clock measurement.

procedure, public :: timer_resume

Resume a paused system_clock wall-clock measurement.

procedure, public :: timer_start

Start a system_clock wall-clock measurement.

procedure, public :: timer_stop

Stop a system_clock wall-clock measurement and update elapsed_time.

procedure, public :: timer_write

Append elapsed_time to a text file.


Functions

private pure function days_from_civil(y, m, d) result(days)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Convert a civil calendar date to days since the Unix epoch.

Read more…

Arguments

Type IntentOptional Attributes Name
integer(kind=ik), intent(in) :: y

Civil year in the proleptic Gregorian calendar.

integer(kind=ik), intent(in) :: m

Civil month in the range 1 to 12.

integer(kind=ik), intent(in) :: d

Civil day of month.

Return Value integer(kind=ik)

private pure function epoch_seconds_utc(values) result(t)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Convert date_and_time values to UTC epoch seconds.

Read more…

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: values(8)

Eight-element result from date_and_time(values=...).

Return Value real(kind=rk)

private pure function ticks_diff(t_end, t_start, t_max) result(dt)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Compute the elapsed tick count between two system_clock values.

Read more…

Arguments

Type IntentOptional Attributes Name
integer(kind=ik), intent(in) :: t_end

End tick captured from system_clock.

integer(kind=ik), intent(in) :: t_start

Start tick captured from system_clock.

integer(kind=ik), intent(in) :: t_max

Maximum count returned by system_clock; non-positive means unknown.

Return Value integer(kind=ik)


Subroutines

private subroutine ctimer_start(this)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Start the CPU-time timer.

Read more…

Arguments

Type IntentOptional Attributes Name
class(timer), intent(inout) :: this

Timer instance whose CPU-time state is initialized.

private subroutine ctimer_stop(this, nloops, message, print, color, rfmt)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Stop the CPU-time timer and store elapsed CPU seconds.

Read more…

Arguments

Type IntentOptional Attributes Name
class(timer), intent(inout) :: this

Timer instance previously started with ctimer_start.

integer, intent(in), optional :: nloops

Optional positive loop count used to compute average CPU time.

character(len=*), intent(in), optional :: message

Optional output label; defaults to CPU time:.

logical, intent(in), optional :: print

Optional output switch; defaults to .true..

character(len=*), intent(in), optional :: color

Optional FACE foreground color used for printed output.

character(len=*), intent(in), optional :: rfmt

Optional Fortran real edit descriptor used for printed output.

private subroutine ctimer_write(this, file_name)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Append the last CPU-time result to a text file.

Read more…

Arguments

Type IntentOptional Attributes Name
class(timer), intent(in) :: this

Timer instance whose cpu_time value is written.

character(len=*), intent(in) :: file_name

Path to the output text file.

private subroutine dtimer_start(this)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Start the date_and_time wall-clock timer.

Read more…

Arguments

Type IntentOptional Attributes Name
class(timer), intent(inout) :: this

Timer instance whose date/time timing state is initialized.

private subroutine dtimer_stop(this, nloops, message, print, color, rfmt)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Stop the date_and_time timer and store elapsed seconds.

Read more…

Arguments

Type IntentOptional Attributes Name
class(timer), intent(inout) :: this

Timer instance previously started with dtimer_start.

integer, intent(in), optional :: nloops

Optional positive loop count used to compute average date/time duration.

character(len=*), intent(in), optional :: message

Optional output label; defaults to Elapsed time:.

logical, intent(in), optional :: print

Optional output switch; defaults to .true..

character(len=*), intent(in), optional :: color

Optional FACE foreground color used for printed output.

character(len=*), intent(in), optional :: rfmt

Optional Fortran real edit descriptor used for printed output.

private subroutine dtimer_write(this, file_name)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Append the last date_and_time result to a text file.

Read more…

Arguments

Type IntentOptional Attributes Name
class(timer), intent(in) :: this

Timer instance whose elapsed_dtime value is written.

character(len=*), intent(in) :: file_name

Path to the output text file.

private subroutine finalize_timing(value_raw, nloops, value_out, default_label, message, print, color, rfmt, ok)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Validate, average, and optionally print a measured duration.

Read more…

Arguments

Type IntentOptional Attributes Name
real(kind=rk), intent(in) :: value_raw

Raw measured duration in seconds.

integer, intent(in), optional :: nloops

Optional positive loop count used to compute an average time per loop.

real(kind=rk), intent(out) :: value_out

Validated duration in seconds, averaged by nloops when present.

character(len=*), intent(in) :: default_label

Backend-specific output label used when message is absent.

character(len=*), intent(in), optional :: message

Optional output label printed before the timing value.

logical, intent(in), optional :: print

Optional output switch; defaults to .true..

character(len=*), intent(in), optional :: color

Optional FACE foreground color for the printed label and unit suffix.

character(len=*), intent(in), optional :: rfmt

Optional Fortran real edit descriptor used by print_time.

logical, intent(out) :: ok

True when finalization succeeds and the caller may store value_out.

private subroutine print_time(time, message, color, rfmt, ok)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Print a formatted timing value.

Read more…

Arguments

Type IntentOptional Attributes Name
real(kind=rk), intent(in) :: time

Timing value in seconds.

character(len=*), intent(in) :: message

Label printed before time.

character(len=*), intent(in), optional :: color

Optional FACE foreground color; defaults to blue.

character(len=*), intent(in), optional :: rfmt

Optional Fortran real edit descriptor; defaults to F16.9.

logical, intent(out) :: ok

True when the formatted write completed successfully.

private subroutine timer_pause(this)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Pause a running system_clock wall-clock timer.

Read more…

Arguments

Type IntentOptional Attributes Name
class(timer), intent(inout) :: this

Running wall-clock timer instance to pause.

private subroutine timer_resume(this)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Resume a paused system_clock wall-clock timer.

Read more…

Arguments

Type IntentOptional Attributes Name
class(timer), intent(inout) :: this

Paused wall-clock timer instance to resume.

private subroutine timer_start(this)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Start the system_clock wall-clock timer.

Read more…

Arguments

Type IntentOptional Attributes Name
class(timer), intent(inout) :: this

Timer instance whose wall-clock state is initialized.

private subroutine timer_stop(this, nloops, message, print, color, rfmt)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Stop the system_clock wall-clock timer and store elapsed seconds.

Read more…

Arguments

Type IntentOptional Attributes Name
class(timer), intent(inout) :: this

Timer instance previously started with timer_start.

integer, intent(in), optional :: nloops

Optional positive loop count used to compute average elapsed time.

character(len=*), intent(in), optional :: message

Optional output label; defaults to Elapsed time:.

logical, intent(in), optional :: print

Optional output switch; defaults to .true..

character(len=*), intent(in), optional :: color

Optional FACE foreground color used for printed output.

character(len=*), intent(in), optional :: rfmt

Optional Fortran real edit descriptor used for printed output.

private subroutine timer_write(this, file_name)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Append the last system_clock result to a text file.

Read more…

Arguments

Type IntentOptional Attributes Name
class(timer), intent(in) :: this

Timer instance whose elapsed_time value is written.

character(len=*), intent(in) :: file_name

Path to the output text file.

private subroutine write_to_file(time, file_name)

Author
Seyed Ali Ghasemi
License
BSD 3-Clause

Append a timing value to a text file.

Read more…

Arguments

Type IntentOptional Attributes Name
real(kind=rk), intent(in) :: time

Timing value in seconds to write.

character(len=*), intent(in) :: file_name

Path to the output text file.