Fortran writing to `/dev/null` or `NUL`

Writing to /dev/null can save significant time on legacy Fortran programs that dump unneeded data to disk. This is lower risk than making modifications to non-standard compliant code, and certainly less programmer time consuming. I have seen 10x speedup from programs that had unneeded intensive file writing. It's also particularly useful for Fortran code called from Python via f2py.

Null filename

The null filename per OS is:

  • Linux, Mac, BSD, Cygwin, Windows Subsystem for Linux: /dev/null
  • native Windows: NUL

Example

Null-writing Fortran: devnull.f90

The test below has /dev/null 3x - 5x faster than scratch file, even with fast SSD.

use, intrinsic:: iso_fortran_env, only: int64
implicit none

integer(int64) :: tic,toc,rate
character(*), parameter :: nulunix='/dev/null', nulwin='NUL',fout='out.txt'
integer, parameter :: N=1000
integer :: ios,u,i
real :: tnul, tscr

! --- benchmark NUL
call system_clock(tic,count_rate=rate)

open(newunit=u,file=nulunix,status='replace',iostat=ios, action='write')
if (ios /= 0) open(newunit=u,file=nulwin,status='replace',iostat=ios, action='write')
if (ios /= 0) error stop 'could not open a NULL file handle'

do i = 1,N
    write(u,*) 'blah blah blah'
    flush(u)
enddo
close(u)
call system_clock(toc)

tnul = (toc-tic)/real(rate)

print *,tnul,' seconds to write to NUL'

!---- benchmark scratch
call system_clock(tic)
open(newunit=u, status='scratch')
do i = 1,N
    write(u,*) 'blah blah blah'
    flush(u)
enddo
close(u)
call system_clock(toc)

tscr = (toc-tic)/real(rate)


print *,tscr,' seconds to write to scratch file'


print '(A,F7.3,A)','NUL is ',tscr/tnul,' times faster than scratch.'

end program