使用dtime或time命令打印出总运行时间(挂钟)?

时间:2012-03-04 21:40:56

标签: time fortran

我正在尝试使用ifort评估模型性能,但打印输出没有正确显示。

这是我的代码。请告诉我如何完成这项任务。

! Time the run

      call dtime(timearray, telapse)

! End of the simulation

      call dtime(timearray, telapse)
      call print_runtime(telapse, ctime)

      subroutine print_runtime(telapse, ctime)

      implicit none
      real*4    telapse 
      character*8 ctime(2)
      integer*2 RunDays,
     >          RunHours,
     >          RunMins,
     >          RunSecs
      character*40 msgstr, rtfname 
      parameter (msgstr = ' *** Total run time (wallclock) was ')
      parameter (rtfname = 'runtime.txt')

! Now convert telapse from seconds to DD HH:MM:SS

      RunDays  = INT (telapse / 86400.0)
      telapse  = telapse - (RunDays * 86400.0)
      RunHours = INT (telapse / 3600.0)
      telapse  = telapse - (RunHours * 3600.0)
      RunMins  = INT (telapse / 60.0)
      RunSecs  = NINT (telapse - (RunMins * 60.0))

      if (RunDays  .GT. 0) then
          write (*,1) msgstr, RunDays, RunHours, RunMins, RunSecs
 1        format (A36, I2, 'days, ', I2.2, ':', I2.2, ':', I2.2, ' hh:mm:ss ***')

      else if (RunHours .GT. 0) then
          write (*,2) msgstr, RunHours, RunMins, RunSecs
 2        format (A36, I2, ':', I2.2, ':', I2.2, ' hh:mm:ss ***') 

      else if (RunMins .GT. 0) then
          write (*,3) msgstr, RunMins, RunSecs
 3        format (A36, I2, ':', I2.2, ' mm:ss ***') 

      else
          write (*,4) msgstr, telapse
 4        format (A36, F7.4, ' seconds ***') 

      endif
      return
      end 

目前,我的打印输出显示“*** Total run time (wallclock) was ******* seconds ***” 如何在这里显示正确的总运行时间(挂钟)? 谢谢。 迈克尔

1 个答案:

答案 0 :(得分:5)

以下代码片段显示如何以秒为单位输出经过的挂钟时间。这与消耗的CPU时间不同,可能不是衡量代码性能的好方法。 DTIME是运行时而不是挂钟时间,也是一个扩展。如果你想要cpu rutime语言标准提供cpu_time。您从秒转换的代码看起来没问题。

integer :: clck_counts_beg, clck_counts_end, clck_rate
call system_clock ( clck_counts_beg, clck_rate )
your code
call system_clock ( clck_counts_end, clck_rate )
write (*, *)  (clck_counts_end - clck_counts_beg) / real (clck_rate)

CPU时间示例,也以秒为单位

real ::  beg_cpu_time, end_cpu_time
call cpu_time (beg_cpu_time)
your code
call cpu_time (end_cpu_time)
write (*, *) end_cpu_time - beg_cpu_time