Fortran 计时函数
etime(fortran 77)
例:call etime(tarray,t0)
    …………….
    Call etime(tarray,t1)
print *, ‘ Elapsed time:’,t1-t0
system_clock (fortran 90)
cpu_time (fortran 95)
例:call cpu_time(t0)
    …………….
    call cpu_time(t1)
Print *, ‘ Elapsed time:’, t1-t0
函数的具体参数可以参看编译软件Compaq Visual Fortran中的帮助文档
和下面这段程序及其运行结果
!关于计算程序所耗费的时间函数,具体可以 参看帮助文档
program systemclock
  integer::    ic, crate, cmax
  integer(2) :: ic2, crate2, cmax2
  integer(4) :: ic4, crate4, cmax4
  integer::t1,t2
  call system_clock(t1)
  call system_clock(count=ic, count_rate=crate, count_max=cmax)
  call system_clock(count=ic2, count_rate=crate2, count_max=cmax2)
  call system_clock(count=ic4, count_rate=crate4, count_max=cmax4)
  call system_clock(t2)
  print *, t1, t2trunc函数使用时间
  print *, ic, crate, cmax
  print *, ic2, crate2, cmax2
  print *, ic4, crate4, cmax4
  end program systemclock
过一会儿再运行一下这段程序,其结果是
可见,此台计算机上编译软件Compaq Visual Fortran的默认的integer是integer(4),计数率是10000(每秒计数10000次),最大计数值是2147483647。计数是从1970年1月1日0时0分0秒开始,当超过最大计算值则溢出,又从0开始。
该计数器从0到计满溢出,对应的时间是
2147483647/10000=2.1475e+005秒
2147483647/10000/60=3.5791e+003分
2147483647/10000/60/60=59.6523小时
计算执行一段程序所需时间的方法:
Integer :: t0, t1  !    t0 , 存放开始执行某段程序前计数器的值
              !    t1, 存放执行某段程序后计数器的值
call system_clock(t0)
………………..
…………………..
call system_clock(t1)
Print*, “the elapsed time (unit: second) = ” ,  (t1-t0)/10000
SYSTEM_CLOCK
Intrinsic Subroutine: Returns integer data from a real-time clock.
Syntax
CALL SYSTEM_CLOCK ([count] [, count_rate] [, count_max])
count
(Optional; output) Must be scalar and of type default integer. It is set to a value based on the current value of the processor clock. The value is increased by one for each clock coun
t until the value count_max is reached, and is reset to zero at the next count. (count lies in the range 0 to count_max.)
count_rate
(Optional; output) Must be scalar and of type default integer. It is set to the number of processor clock counts per second.
If default integer is INTEGER(2), count_rate is 1000. If default integer is INTEGER(4), count_rate is 10000. If default integer is INTEGER(8), count_rate is 1000000.
count_max
(Optional; output) Must be scalar and of type default integer. It is set to the maximum value that count can have, HUGE(0).
SYSTEM_CLOCK returns the number of seconds from 00:00 Coordinated Universal Time (CUT) on 1 JAN 1970. The number is returned with no bias. To get the elapsed time, you must call SYSTEM_CLOCK twice, and subtract the starting time value from the ending time value.
Compatibility
CONSOLE STANDARD GRAPHICS QUICKWIN GRAPHICS WINDOWS DLL LIB
See Also: DATE_AND_TIME, HUGE, GETTIM
Examples
Consider the following:
  integer(2) :: ic2, crate2, cmax2
  integer(4) :: ic4, crate4, cmax4
  call system_clock(count=ic2, count_rate=crate2, count_max=cmax2)
  call system_clock(count=ic4, count_rate=crate4, count_max=cmax4)
  print *, ic2, crate2, cmax2
  print *, ic4, crate4, cmax4
  end
This program was run on Thursday Dec 11, 1997 at 14:23:55 EST and produced the following output:
  13880  1000  32767
  1129498807      10000  2147483647

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。