直觉上,我认为要比较两个值,速度应该是逻辑>整数>真,所以我做了这个测试程序:
program t
logical :: a=.true.
real :: b=1.021341
integer :: c=1
real(kind=16) :: start, finish,test
call cpu_time(start)
test=0.0
do while (test<1000000.)
if (a.eqv.a) then
test=test+0.01
endif
enddo
call cpu_time(finish)
print '("Time for compare logical= ",f6.3," seconds.")',finish-start
call cpu_time(start)
test=0.0
do while (test<1000000.)
if (b.eq.b) then
test=test+0.01
endif
enddo
call cpu_time(finish)
print '("Time for compare real= ",f6.3," seconds.")',finish-start
call cpu_time(start)
test=0.0
do while (test<1000000.)
if (c.eq.c) then
test=test+0.01
endif
enddo
call cpu_time(finish)
print '("Time for compare integer= ",f6.3," seconds.")',finish-start
end program
但输出是
Time for compare logical= 2.228 seconds.
Time for compare real= 2.200 seconds.
Time for compare integer= 2.200 seconds.
看起来他们没有显着差异。为什么我认为逻辑应该是最快的。我使用gfortran 5.4.0。我的操作系统是ubuntu 16.04。