I have often observed that unformatted file writing (as shown below) is way faster than formatted file writing (also as shown below) in Fortran 90.
Unformatted file writing
OPEN ( Unit=86, File='out.dat', Form='unformatted', Action='Write')
WRITE (86) A, B, C
CLOSE (86)
Formatted file writing
OPEN ( Unit=86, File='out.dat', Form='formatted', Action='Write')
DO ii = 1,N
DO jj = 1,N
WRITE (*,86) A(ii,jj), B(ii,jj), C(ii,jj)
END DO
END DO
CLOSE (86)
Where A,B,C are two-dimensional arrays of size(N,N). I found that in this case the difference in CPU_Time
was around 12s.
Why is there a huge difference? Is it just the time taken by looping in formatted procedure?