0

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?

  • 2
    These two cases do massively different things, even beyond the formatted/unformatted change. If you compare where there is simply that one difference you will still see, perhaps, the unformatted write is faster, but which aspects do you want to understand? – francescalus Nov 08 '16 at 14:07
  • Ya. True. I have observed that. I am just wondering how and why unformatted write is faster? – Ramanathan Varadharajan Nov 08 '16 at 14:11
  • Also, your `write` statements are wrong: `write(86) A, B, C` in the first case, and `write(86,*) ...` (or `write(fmt=*,unit=86) ...`) in the second. – francescalus Nov 08 '16 at 14:24

1 Answers1

1

Notice that in one case you write the whole A first, then whole B then whole C. In the second you are writing A11, B11, C11, A21, B21, C21, ... That is a difference, but the impact will be small.

It takes a lot of CPU time to convert the numbers from the binary memory representation to human readable numbers. That is mostly what makes it slower in formatted case. Also the file is larger so writing it takes more time.