2

我通过system_clock以下方式使用 Fortran90(用 gfortran 编译)的函数:

   ! Variables for clock
   integer count_0, count_1
   integer count_rate, count_max
   double precision time_init, time_final, elapsed_time

   ! Starting time
   call system_clock(count_0, count_rate, count_max)
   time_init = count_0*1.0/count_rate

   .... Main code

   ! Ending time
   call system_clock(count_1, count_rate, count_max)
   time_final = count_1*1.0/count_rate
   ! Elapsed time
   elapsed_time = time_final - time_init

   ! Write elapsed time
   write(*,1003) int(elapsed_time),elapsed_time-int(elapsed_time)

1003 format('  Wall Clock = ',i0,f0.9)

我想知道我是否正确使用了这个功能。确实,我没有为 and 指定值count_ratecount_max但我猜有默认值。此外,似乎我必须考虑count_0count_1超过count_max价值的情况,不是吗。如您所见,为了漂亮的格式,我将秒数和经过时间的小数部分分开。

4

1 回答 1

3

从阅读中看起来是正确的。然而,如果没有看到输出,很难找出任何代码的正确性。

我建议使用更大的整数 ( integer(int64))。int64在模块中定义iso_fortran_env,在 Fortran 90 中可以使用selected_int_kind。它是特定于编译器的,但是使用更大的整数可能会变得更高count_max和更好。count_rate至少对于像 gfortran 和 Intel 这样的常见编译器会发生这种情况。

我认为您无法对其进行有用的监视count和比较。count_max当我使用如上所示的较大整数时,我从来没有需要。我可以想象某种方式(与 相比count_max/2),但这会很笨拙。无论如何,您都无法重新启动计数器。

你可以做的是写一个小程序来打印最大次数

use iso_fortran_env, only: int32, int64, real64

integer(int32) :: count_max, count_rate

call system_clock(count_max=count_max, count_rate=count_rate)

write(*,*) "Maximum time:", real(count_max, real64) / count_rate

end

试试上面的integer(int32)and integer(int64)and alsointeger并观察编译器的这些选择可能的最大时间。

对我在 Linux x86_64 上使用 gfortran 而言,32 位整数给了我将近 25 天的最大时间,而 64 位整数允许 292 年。使用 64 位整数的时钟分辨率也更好(1 ms 对 1 ns)。

于 2017-10-09T05:49:16.340 回答