【问题标题】:Measure time taken during Fortran program测量 Fortran 程序所用时间
【发布时间】:2012-09-15 06:41:59
【问题描述】:

我最近开始学习 Fortran 以获得乐趣,我想知道是否有任何简单的方法来显示执行我的代码所花费的时间。这只是一个数到一百万的简单循环,我想看看需要多长时间。

如果有帮助,这是我正在使用的代码:

program count
    implicit none
    integer :: i

    do i=0,1000000
        print*,i
    end do
end program count

我在 Linux 上使用 gFortran 作为编译器。我正在使用 Geany 作为文本编辑器。

【问题讨论】:

    标签: fortran timing


    【解决方案1】:

    在 Fortran 90 或更高版本中,使用 SYSTEM_CLOCK 内部子例程:

    call system_clock(beginning, rate)
    result = do_computation()
    call system_clock(end)
    print *, "elapsed time: ", real(end - beginning) / real(rate)
    

    【讨论】:

      【解决方案2】:

      一个简单的方法是call cpu_time(timeVariable)

      示例:

      program ElapsedTime
        implicit none
      
        integer :: i
        real :: startTime, stopTime
      
        call cpu_time(startTime)
      
        do i = 1, 1000000000
           if(mod(i,10000000)==0)then
              print *,"iter: ", i
           endif
        end do
      
        call cpu_time(stopTime)
        
        write(*, '(A, F8.6)') 'Elapsed time, s : ',  (stopTime - startTime)
      
      end program 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-30
        相关资源
        最近更新 更多