How can I set an array to the contents of a file?

Fortran reallocation is really ineffective if done one-by-one (see this topic) because it allocates new memory, copies the data and deallocates the old chunk. So it pretty much uses 2n as @simong said even w/o any linked lists.

It can be, however, made much, much more effectively by reallocating the memory in bigger chunks. See the snippet below.

code
program testit
  implicit none
  integer, parameter :: chunk=1024
  integer :: i, lun, ios, cursize, npoints=0, temp(chunk)=0
  integer,allocatable :: ivals(:)
  if(allocated(ivals))deallocate(ivals)
  allocate(ivals(chunk))
  cursize = chunk
  open(file='sample.txt',newunit=lun)
  do
    read(lun,'(i256)',advance='no',iostat=ios) i
    if(is_iostat_eor(ios).or.ios.eq.0)then
      npoints = npoints+1
      if (npoints > cursize) then
        ivals=[ivals,temp]
        cursize = cursize + chunk
      endif
      ivals(npoints) = i
    else
      exit
    endif
  enddo
  if (npoints < cursize) then
    ivals = [ivals(1:npoints)]
  endif
  write(*,*)'size=',size(ivals)
end program testit

The difference is huge. I tried the @urbanjost version on my 6 years old macbook pro (2,8 GHz Quad-Core Intel Core i7) and a sample file containing just below 200,000 random integers (1.1 MB). It takes 14 seconds to read them. The above code takes 0.1 sec.

2 Likes