title: Writing to file in Fortran do loop
body: <p>I am just starting to learn fortran and I have made a simple program, implementing Eulers method. I noticed that the time used by the <code>do</code> loop is fairly similar to, for example, the same program in Matlab.</p>
  
  <p>In my Fortran program, I had a write to a file inside this <code>do</code> loop. When I removed it the speed drastically improved, as expected. Then I added a second <code>do</code> loop, where I place the writing and measured the time required by both loops combined: </p>
  
  <pre><code>open(unit=1, file='data.dat')
  write(1,*) t(1),' ', y(1)
  ! Calculating
  call cpu_time(start)
  do i = 2,n
      t(i) = t(i-1) + dt
      y(i) = y(i-1) + dt*(t(i-1)*(y(i-1)))
      !write(1,*) t(i),' ', y(i)
  end do
  call cpu_time(finish)
  print  '("Time = ",f7.5," seconds.")',finish-start
  call cpu_time(start)
  do i = 2,n
      write(1,*) t(i),' ',y(i)
  end do
   call cpu_time(finish)
  print  '("Time = ",f7.5," seconds.")',finish-start
  </code></pre>
  
  <p>The time for a given <code>n</code> would take approximately 10-15 times longer on the last <code>do</code> loop compared to the first one.</p>
  
  <p>So, my question is, is there a better way to write my data to a file?</p>