在网上找了下,有很多种办法,结合自己试验,总结如下:(仅仅针对部分环境试验了)
总体来说方法有如下几种:
1. gettim
2. system_clock
3. cpu_time
下面分别介绍
1. gettim
需要使用 DFLIB 模块
使用如下:
总体来说方法有如下几种:
1. gettim
2. system_clock
3. cpu_time
下面分别介绍
1. gettim
需要使用 DFLIB 模块
使用如下:
-
CALL GETTIM (ihr, imin, isec, i100th)
-
ihr
(Output) INTEGER(2). Hour (0-23).
-
imin
(Output) INTEGER(2). Minute (0-59).
-
isec
(Output) INTEGER(2). Second (0-59).
-
i100th
(Output) INTEGER(2). Hundredths of a second (0-99).
例子:
! Program to demonstrate GETDAT and GETTIM
USE DFLIB
INTEGER(2) tmphour, tmpminute, tmpsecond, tmphund
CALL GETDAT(tmpyear, tmpmonth, tmpday)
CALL GETTIM(tmphour, tmpminute, tmpsecond, tmphund)
WRITE (*, 901) tmphour,tmpminute,tmpsecond,tmphund
901 FORMAT(I2, ':', I2.2, ':', I2.2, ':', I2.2)
END
在compaq visual fortran下可以运行,但是在Ubuntu g77下无法编译通过。
2. system_clock
用法:
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 count 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).
这个的返回值的单位和integer的位数有关系,也就是和系统有关系。在使用时要注意这一点。
integer :: time(0:1),timediff
call system_clock(time(0))
C YOUR PROGRAM HERE
call system_clock(time(1))
timediff = time(1)-time(0)
在compaq 和 g77下都可以编译通过
3. cpu_time
用法:
CALL CPU_TIME (time) 返回值的单位为秒
time
Must be scalar and of type real. It is an INTENT(OUT)
argument. 必须是实数或者标量
If a meaningful time cannot be returned, a processor-dependent negative value
is returned. 返回值为负的时候要注意了。
REAL time_begin, time_end
...
CALL CPU_TIME ( time_begin )
!
!task to be timed
!
CALL CPU_TIME ( time_end )
PRINT *, 'Time of operation was ', time_end - time_begin, ' seconds'
compaq和g77均编译通过。
以上3者在compaq下都能编译通过。 三者得到的时间差不多。推荐使用cpu_time。system_clock由于
单位不确定,不是很好用。gettim的变量太多。
在g77下,后两者通过编译。同样推荐使用cpu_time。cpu_time是g95加入的语法,不知道为什么g77
也可以编译通过,求解释。
初步结果就是这样了,以后有新发现再更新。欢迎转载,转载请注明出处。
http://hi.baidu.com/zhaolegend/blog/item/9117dfcef8d43b8bc9176852.html