Fortran三种计时方法

方法1:

call date_and_time(charecter_date(len=8),charecter_time(len=10))
需要日期可以用这种方法

方法2:

call cpu_time(real_time)
最简单的方法,但只适用于单线程,
如果是多线程运行,得到的结果是所以线程时间的和!

方法3:

call system_clock(int_time)
整形,单位是毫秒,多线程用这个方法

代码:
program main

    implicit none 

    real(kind=4)    :: t1,t2
    integer         :: int1,int2
    integer         :: ii,jj,kk
    character(len=10)   :: time1,time2
    character(len=8)    :: thedate
    character(len=12)   :: thetime


    write(*,*) 'date_and_time:'
    call date_and_time(thedate,time1)

    do ii = 1,10000
        do jj = 1,100000
            kk = jj 
        enddo
    enddo 

    call date_and_time(thedate,time2)

    write(*,*) 'date: ',thedate
    write(*,*) 'time1: ',time1
    write(*,*) 'time2: ',time2
    write(*,*) 'time2 is ',time2(1:2) // ':' // time2(3:4) // ':' // time2(5:10)

    read(time1,*) t1
    read(time2,*) t2

    write(*,*) 'time:', t2-t1

    write(*,*) ' '
    write(*,*) 'cpu_time:'

    call cpu_time(t1)

    do ii = 1,10000
        do jj = 1,100000
            kk = jj 
        enddo
    enddo 

    call cpu_time(t2)

    write(*,*) 't1:',t1 
    write(*,*) 't2:',t2 
    write(*,*) 't2-t1=',t2-t1

    write(*,*) ' '
    write(*,*) 'system_clock:'

    call system_clock(int1)

    do ii = 1,10000
        do jj = 1,100000
            kk = jj 
        enddo
    enddo 

    call system_clock(int2)

    write(*,*) 'int1: ', int1
    write(*,*) 'int2: ', int2
    write(*,*) 'unit is ms'
    write(*,*) '(int2-int1)/1000. is ' , (int2-int1)/1000.
    
end

结果:

 date_and_time:
 date: 20190111
 time1: 112638.795
 time2: 112642.070
 time2 is 11:26:42.070
 time:   3.27343750    
  
 cpu_time:
 t1:   3.27646708    
 t2:   6.54442978    
 t2-t1=   3.26796269    
  
 system_clock:
 int1:    120586558
 int2:    120589826
 unit is ms
 (int2-int1)/1000. is    3.26799989  

你可能感兴趣的:(Fortran)