I tried to improve his code
Compiler: ifort
Flags: -O3
- I didn’t understand why he needed two subroutines
- He also adds some overhead with the subroutine call inside a loop
- I added some comments and spaces to make the code readable
- I also graph the data at the end
If possible, how can I improve his code further ?
program simple_num_prob
implicit none
integer :: i
integer,parameter :: tf=25,n=10000
real,allocatable :: t(:),u(:)
real :: dt
real(kind=8) :: t1,t2
character(len=*),parameter :: plt_file='simple_num_prob.gp'
call cpu_time(t1)
allocate(t(n))
allocate(u(n))
! Assigning step value
dt=real(tf)/real(n)
! Initial u value assigned
u(1)=1.0E-5
! Population of t vector
do i=1,n
t(i)=real(i)
end do
! Calculating and populating the function vector
do i=1,n-1
u(i+1) = u(i) + (dt*(u(i)*(1.0-u(i))))
end do
call cpu_time(t2)
print *, "Elapsed time",t2-t1
!writing to file
open (unit=1,file='simple_num_prob.dat')
do i=1,n
write(1,*) t(i),' ',u(i)
end do
close(1)
print *, "File written !"
call cpu_time(t2)
print *, "Elapsed time with writing to file",t2-t1
!deallocation
deallocate(t)
deallocate(u)
call execute_command_line('gnuplot ' // plt_file)
end program simple_num_prob
Gnuplot file:
plot 'simple_num_prob.dat' using 1:2
pause -1