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.
| 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 |
| integer, | public, | parameter | :: | rk | = | real64 |
Real kind used by the public timing results and internal real-valued
calculations. It is currently mapped to |
Timing object with independent state for each supported backend.
| 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 |
|
| real(kind=rk), | public | :: | elapsed_time | = | 0.0_rk |
Most recent successful |
|
| integer(kind=ik), | private | :: | clock_max | = | 0_ik |
Cached maximum |
|
| integer(kind=ik), | private | :: | clock_rate | = | 0_ik |
Cached |
|
| integer(kind=ik), | private | :: | clock_start | = | 0_ik |
|
|
| real(kind=rk), | private | :: | cpu_start | = | 0.0_rk |
CPU time, in seconds, captured by |
|
| real(kind=rk), | private | :: | dtime_start_utc | = | 0.0_rk |
UTC epoch seconds computed from |
|
| logical, | private | :: | is_cpu_started | = | .false. |
True after |
|
| logical, | private | :: | is_dtime_started | = | .false. |
True after |
|
| logical, | private | :: | is_paused | = | .false. |
True after |
|
| logical, | private | :: | is_started | = | .false. |
True after |
|
| integer(kind=ik), | private | :: | pause_start | = | 0_ik |
|
|
| integer(kind=ik), | private | :: | paused_ticks | = | 0_ik |
Total paused duration, in |
| procedure, public :: ctimer_start | Start a CPU-time measurement. |
| procedure, public :: ctimer_stop | Stop a CPU-time measurement and update |
| procedure, public :: ctimer_write | Append |
| procedure, public :: dtimer_start | Start a |
| procedure, public :: dtimer_stop | Stop a |
| procedure, public :: dtimer_write | Append |
| procedure, public :: timer_pause | Pause a running |
| procedure, public :: timer_resume | Resume a paused |
| procedure, public :: timer_start | Start a |
| procedure, public :: timer_stop | Stop a |
| procedure, public :: timer_write | Append |
Convert a civil calendar date to days since the Unix epoch.
| Type | Intent | Optional | 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. |
Convert date_and_time values to UTC epoch seconds.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(in) | :: | values(8) |
Eight-element result from |
Compute the elapsed tick count between two system_clock values.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=ik), | intent(in) | :: | t_end |
End tick captured from |
||
| integer(kind=ik), | intent(in) | :: | t_start |
Start tick captured from |
||
| integer(kind=ik), | intent(in) | :: | t_max |
Maximum count returned by |
Start the CPU-time timer.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(timer), | intent(inout) | :: | this |
Timer instance whose CPU-time state is initialized. |
Stop the CPU-time timer and store elapsed CPU seconds.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(timer), | intent(inout) | :: | this |
Timer instance previously started with |
||
| 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 |
|
| logical, | intent(in), | optional | :: |
Optional output switch; defaults to |
||
| 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. |
Append the last CPU-time result to a text file.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(timer), | intent(in) | :: | this |
Timer instance whose |
||
| character(len=*), | intent(in) | :: | file_name |
Path to the output text file. |
Start the date_and_time wall-clock timer.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(timer), | intent(inout) | :: | this |
Timer instance whose date/time timing state is initialized. |
Stop the date_and_time timer and store elapsed seconds.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(timer), | intent(inout) | :: | this |
Timer instance previously started with |
||
| 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 |
|
| logical, | intent(in), | optional | :: |
Optional output switch; defaults to |
||
| 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. |
Append the last date_and_time result to a text file.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(timer), | intent(in) | :: | this |
Timer instance whose |
||
| character(len=*), | intent(in) | :: | file_name |
Path to the output text file. |
Validate, average, and optionally print a measured duration.
| Type | Intent | Optional | 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 |
||
| character(len=*), | intent(in) | :: | default_label |
Backend-specific output label used when |
||
| character(len=*), | intent(in), | optional | :: | message |
Optional output label printed before the timing value. |
|
| logical, | intent(in), | optional | :: |
Optional output switch; defaults to |
||
| 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 |
|
| logical, | intent(out) | :: | ok |
True when finalization succeeds and the caller may store |
Print a formatted timing value.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=rk), | intent(in) | :: | time |
Timing value in seconds. |
||
| character(len=*), | intent(in) | :: | message |
Label printed before |
||
| character(len=*), | intent(in), | optional | :: | color |
Optional FACE foreground color; defaults to |
|
| character(len=*), | intent(in), | optional | :: | rfmt |
Optional Fortran real edit descriptor; defaults to |
|
| logical, | intent(out) | :: | ok |
True when the formatted write completed successfully. |
Pause a running system_clock wall-clock timer.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(timer), | intent(inout) | :: | this |
Running wall-clock timer instance to pause. |
Resume a paused system_clock wall-clock timer.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(timer), | intent(inout) | :: | this |
Paused wall-clock timer instance to resume. |
Start the system_clock wall-clock timer.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(timer), | intent(inout) | :: | this |
Timer instance whose wall-clock state is initialized. |
Stop the system_clock wall-clock timer and store elapsed seconds.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(timer), | intent(inout) | :: | this |
Timer instance previously started with |
||
| 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 |
|
| logical, | intent(in), | optional | :: |
Optional output switch; defaults to |
||
| 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. |
Append the last system_clock result to a text file.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(timer), | intent(in) | :: | this |
Timer instance whose |
||
| character(len=*), | intent(in) | :: | file_name |
Path to the output text file. |
Append a timing value to a text file.
| Type | Intent | Optional | 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. |