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 do
loop is fairly similar to, for example, the same program in Matlab.
In my Fortran program, I had a write to a file inside this do
loop. When I removed it the speed drastically improved, as expected. Then I added a second do
loop, where I place the writing and measured the time required by both loops combined:
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
The time for a given n
would take approximately 10-15 times longer on the last do
loop compared to the first one.
So, my question is, is there a better way to write my data to a file?