Timers in Fortran
Our first example demonstrates the use of timers in Fortran. We’ll use the sleep
subroutine from the iso_c_binding
module to simulate timers.
In this Fortran program:
We use the
iso_c_binding
module to access thec_sleep
function, which allows us to pause the program execution.The
timer1
subroutine simulates a timer that waits for 2 seconds usingc_sleep(2)
.The
timer2
subroutine demonstrates a timer that can be cancelled. We use a logical variablestop_timer
to simulate stopping the timer.In the main program, we call both
timer1
andtimer2
.
To run the program:
The output shows that Timer 1 fires after 2 seconds, while Timer 2 is stopped before it has a chance to fire.
Note that Fortran doesn’t have built-in timer or ticker features like some other languages. In real-world applications, you might use system-specific libraries or more advanced timing modules for precise timing operations.