Skip to content

Instantly share code, notes, and snippets.

@zmiimz
Last active October 17, 2016 11:04
Show Gist options
  • Save zmiimz/1e85ea37e3b02226068ad4b3f2ba41c4 to your computer and use it in GitHub Desktop.
Save zmiimz/1e85ea37e3b02226068ad4b3f2ba41c4 to your computer and use it in GitHub Desktop.
pow and sqrt timing in fortran
! gfortran -O3 pow_timing.f90 -o pow_timing.x -mcmodel=medium
!-----------------------------------------------------------------------
!Main program test
!-----------------------------------------------------------------------
program test
implicit none
integer, parameter :: dp = kind(1.d0)
integer :: i
integer, parameter :: s = 100000000
real(dp) :: a
real(dp), dimension(s) :: b1,b2,b3,b4,b5,b6,b7
real(dp), dimension(2) :: time
character(len=11):: fmt = '(A30,F12.6)'
call cpu_time (time(1))
do i = 1, s
a = real(i,dp)
b1(i) = a*a
enddo
call cpu_time (time(2))
write(*,fmt) "a*a elapsed time = ", time(2)-time(1)
call cpu_time (time(1))
do i = 1, s
a = real(i,dp)
b2(i) = a**2
enddo
call cpu_time (time(2))
write(*,fmt) "a**2 elapsed time = ", time(2)-time(1)
call cpu_time (time(1))
do i = 1, s
a = real(i,dp)
b3(i) = a**2.0
enddo
call cpu_time (time(2))
write(*,fmt) "a**2.0 elapsed time = ", time(2)-time(1)
call cpu_time (time(1))
do i = 1, s
a = real(i,dp)
b4(i) = a**2.0_dp
enddo
call cpu_time (time(2))
write(*,fmt) "a**2.0_dp elapsed time = ", time(2)-time(1)
call cpu_time (time(1))
do i = 1, s
a = real(i,dp)
b5(i) = sqrt(a)
enddo
call cpu_time (time(2))
write(*,fmt) "sqrt(a) elapsed time = ", time(2)-time(1)
call cpu_time (time(1))
do i = 1, s
a = real(i,dp)
b6(i) = a**0.5
enddo
call cpu_time (time(2))
write(*,fmt) "a**0.5 elapsed time = ", time(2)-time(1)
call cpu_time (time(1))
do i = 1, s
a = real(i,dp)
b7(i) = a**0.5_dp
enddo
call cpu_time (time(2))
write(*,fmt) "a**0.5_dp elapsed time = ", time(2)-time(1)
! to force bx loops computation
do i = 1, s
if(b1(i) /= b2(i)) stop "error"
if(b1(i) /= b3(i)) stop "error"
if(b1(i) /= b4(i)) stop "error"
if(b5(i) /= b6(i)) stop "error"
if(b5(i) /= b7(i)) stop "error"
enddo
end program test
@zmiimz
Copy link
Author

zmiimz commented Jun 18, 2016

gfortran 6.2.1 20160830

./pow_timing.x

       a*a elapsed time =     0.144000
      a**2 elapsed time =     0.132000
    a**2.0 elapsed time =     0.128000
 a**2.0_dp elapsed time =     0.128000
   sqrt(a) elapsed time =     0.784000
    a**0.5 elapsed time =     7.656000
 a**0.5_dp elapsed time =     7.716000

ifort (IFORT) 17.0.0 20160721

./pow_timing.x

       a*a elapsed time =     0.152000
      a**2 elapsed time =     0.136000
    a**2.0 elapsed time =     0.132000
 a**2.0_dp elapsed time =     0.136000
   sqrt(a) elapsed time =     0.412000
    a**0.5 elapsed time =     0.416000
 a**0.5_dp elapsed time =     0.416000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment