[Patch, fortran] PR35339 Optimize implied do loops in io statements

Thomas Koenig tkoenig@netcologne.de
Mon May 29 14:06:00 GMT 2017


Hi Nicolas,

> gcc@dcm-linux:~/pr/35339> gfortran -O3 perf_test.f90
> gcc@dcm-linux:~/pr/35339> time ./a.out
> 
> real    0m1.879s
> user    0m1.838s
> sys     0m0.040s
> gcc@dcm-linux:~/pr/35339> gfortran-7 -O3 perf_test.f90
> gcc@dcm-linux:~/pr/35339> time ./a.out
> 
> real    0m1.929s
> user    0m1.901s
> sys     0m0.028s

For formatted I/O, the advantage will not be large because
of the CPU time needed for converting numbers to decimal.
People who care about speed in I/O use unformatted I/O,
and there is a big advantage to using an array slice:

$ cat tst.f90
module x
   implicit none
contains
   double precision function v2s(v)
     integer, dimension(8), intent(in) :: v
     v2s = v(8)*1d-3+v(7)+v(6)*60.d0+v(5)*3600.d0
   end function v2s
end module x
program main
   use x
   implicit none
   integer, parameter :: n=10**7
   real, dimension(n) :: a
   real:: t1, t2
   integer :: i
   integer, dimension(8) :: v1, v2

   call random_number(a)
   open(unit=10,file="test.dat",form="unformatted")
   call cpu_time(t1)
   call date_and_time(values=v1)
   write (10) (a(i),i=1,n)
   call date_and_time(values=v2)
   call cpu_time(t2)
   print *,"CPU time implied DO loop = ", t2-t1
   print *,"Wall time = ", v2s(v2)-v2s(v1)

   call cpu_time(t1)
   call date_and_time(values=v1)
   write (10) a(1:i:1)
   call cpu_time(t2)
   call date_and_time(values=v2)
   print *,"CPU time Array= ", t2-t1
   print *,"Wall time = ", v2s(v2)-v2s(v1)
   close(10,status="delete")
end program main

$ gfortran -O3 tst.f90
$ ./a.out
  CPU time implied DO loop =   0.374823004
  Wall time =   0.50600000000122236
  CPU time Array=    6.18300140E-02
  Wall time =   0.10599999999976717
$





More information about the Fortran mailing list