! copyright 2005, J. E. Akin, all rights reserved. function cpu_clock_start () result(start) ! ------------------------------------------------- ! Exercise DATE_AND_TIME and SYSTEM_CLOCK functions. ! to start timing a CPU process ! ------------------------------------------------- ! Warning: reporting rate may be crude, e.g. 0.01 sec ! ! Declare variables. integer :: start, rate character* 8 :: the_date character*10 :: the_time ! ! Call the system clock to start an execution timer. call system_clock ( COUNT = start ) ! call date_and_time ( DATE = the_date ) call date_and_time ( TIME = the_time ) print *, 'The date is ', the_date, & ' and the time is now ', the_time ! Display facts about the system clock. print *, ' ' call system_clock ( COUNT_RATE = rate ) print *, 'System clock runs at', rate, & 'ticks per second' end function ! cpu_clock_start function cpu_clock_time (start) result (seconds) ! ------------------------------------------------- ! Use start (from cpu_clock_start) and SYSTEM_CLOCK ! function to find elapsed CPU run time ! ------------------------------------------------- ! Warning: reporting rate may be crude, e.g. 0.01 sec ! ! Declare variables. integer :: start, finish, rate real :: seconds ! ! Stop the execution timer and report execution time. call system_clock ( COUNT = finish ) call system_clock ( COUNT_RATE = rate ) if ( finish >= start ) then seconds = float( finish - start ) / float( rate ) else seconds = 0.0 end if print *, ' ' print *, 'Job has taken', seconds, 'seconds to execute' print *, ' ' end function ! cpu_clock_time subroutine tic ! ------------------------------------------------- ! Model the matlab tic function, for use with toc ! ------------------------------------------------- ! Useage: call tic ! start clock ! cputime = toc () ! for increment implicit none integer :: start, finish, rate common / tic_toc / start, finish, rate ! a module is better here ! Call the system clock to start an execution timer. call system_clock ( COUNT = start ) ! current value call system_clock ( COUNT_RATE = rate ) ! counts/sec end subroutine tic function toc ( ) ! ------------------------------------------------- ! Model the matlab toc function, for use with tic ! ------------------------------------------------- ! Useage: call tic ! start clock ! cputime = toc () ! for increment implicit none integer :: start, finish, rate common / tic_toc / start, finish, rate ! a module is better real :: toc ! Stop the execution timer call system_clock ( COUNT = finish ) if ( finish .ge. start ) then toc = float( finish - start ) / float( rate ) else toc = 0.0 end if end function toc subroutine date (c, y, m, d) ! ------------------------------------------------- ! Model and improve the matlab date function ! ------------------------------------------------- character* 8 the_date ! required size character* 2, intent(out) :: c ! century character* 2, intent(out) :: y ! year character* 2, intent(out) :: m ! month character* 2, intent(out) :: d ! day call date_and_time ( DATE = the_date ) c = the_date(1:2) y = the_date(3:4) m = the_date(5:6) d = the_date(7:8) end subroutine date subroutine time ( h, m, s, zh, zm) ! ------------------------------------------------- ! return the current time in this zone ! (illustrates converting strings to numbers) ! ------------------------------------------------- character* 5 :: the_zone ! required size character*10 :: the_time ! required size integer :: h, m, zh, zm real :: s call date_and_time ( ZONE = the_zone ) call date_and_time ( TIME = the_time ) ! convert strings to numbers, via "internal" file read (the_time(1:2 ), '(i2)') h ! hour read (the_time(3:4), '(i2)') m ! minute read (the_time(5:10), '(f5.3)') s ! second read (the_zone(1:3 ), '(i3)') zh ! hour correction read (the_zone(4:5), '(i2)') zm ! minute correction end subroutine time