By strange coincidence, I too have been surprised with the poor performance writing to binary direct access files. I've not used this approach before, but I was aware that sometimes my programming style does not led itself to speed - lots of writing and then reading the data by different program units - so I thought I look at this some wet winter evening.
On my machine the code below, takes 47 ms to allocate and populate the R4 1000x1000 array A, 17.2 s to write it to the direct access file, and 296 ms to recover same the data into array B.
Is this typical performance for writing, or am I doing something silly here?
Cheers Ken
PROGRAM TEST
IMPLICIT NONE
INTEGER RECLEN, NROW, NCOL, I, J, RECORD
REAL, ALLOCATABLE:: A(:,:), B(:,:)
REAL T1,T2,T3,T4
CALL CLOCK@(T1)
NROW = 1000
NCOL = 1000
ALLOCATE(A(NROW,NCOL))
DO J = 1, NCOL, 1
DO I = 1, NROW, 1
A(I,J) = (J-1)*NCOL + I
END DO
END DO
CALL CLOCK@(T2)
WRITE(6,*)'TIME ALLOCATE AND POLULATE ARRAY IN MEMORY', T2-T1
INQUIRE(IOLENGTH=RECLEN)A(1,1)
OPEN(UNIT=10,FILE='data1.bin',FORM='UNFORMATTED',ACCESS='DIRECT',
& RECL=reclen)
DO J = 1, NCOL, 1
DO I = 1, NROW, 1
RECORD = (J-1)*NCOL + I
WRITE(10,REC=RECORD) A(I,J)
END DO
END DO
CALL CLOCK@(T3)
WRITE(6,*)'TIME TO WRITE ARRAY TO DIRECT ACCESS FILE',T3-T2
ALLOCATE(B(NROW,NCOL))
DO J = 1, NCOL, 1
DO I = 1, NROW, 1
RECORD = (J-1)*NCOL + I
READ(10,REC=RECORD) B(I,J)
END DO
END DO
CALL CLOCK@(T4)
WRITE(6,*)'TIME TO READ ARRAY FROM DIRECT ACCESS FILE',T4-T3
CLOSE(UNIT=10,STATUS='DELETE')
END