Fortran Coder

标题: 求取更快的運算方式 [打印本页]

作者: max533    时间: 2014-10-30 12:05
标题: 求取更快的運算方式
這個程式每次跑完要花掉我一天多的時間,是不是修改其中那些語法或者用矩陣讀入會比較快?
因為檔案樹快要接近50個,所以跑完要花許久時間。希望各位高手給予幫助 > <

程式碼如下:
[Fortran] 纯文本查看 复制代码
program ZWD_gamit                                                         
implicit none                                                            
                                                                          
  integer :: stat,stat2,stat3,stat4,yr,dayofyear,hr                       
  character :: filename_ztd*12,filename_zhd*12,filename_zwd*12            
  character :: c_time_all_ztd*12,c_time_all_zhd*12,c_zhd_mm*12            
  real*8 :: time_all_ztd,ztd_m,Tsr_deg,Ps_hpa,zhd_mm,ztd_mm,zwd_mm           
                                                                          
  open(11,file="list.txt")                                                
  close(11,status="delete")                                               
  open(22,file="list2.txt")                                               
  close(22,status="delete")                                               
  call system('for %f in (*_all.ztd) do echo %f >> list.txt')            
  call system('for %f in (*_all.zhd) do echo %f >> list2.txt')            
  open (11,file="list.txt")                                               
  open (22,file="list2.txt")                                             
                                                                          
  stat=0                                                                  
  stat2=0                                                                 
  do while (stat==0 .and. stat2==0)                                       
    read (11,"(a12)",iostat=stat) filename_ztd                           
    read (22,"(a12)",iostat=stat2) filename_zhd                           
    if (stat/=0 .and. stat2/=0) exit                                      
    open (33,file=filename_ztd)                                          
    open (44,file=filename_zhd)                                          
    filename_zwd=filename_ztd(1:8)//".zwd"                                
    open (55,file=filename_zwd)                                          
                                                                          
    stat3=0                                                               
    do while (stat3==0)                                                   
      read (33,50,iostat=stat3) c_time_all_ztd,ztd_m,yr,dayofyear,hr      
50    format (a12,4x,f6.1,3x,i4,3x,i3.3,3x,i2.2)                          
      if (stat3/=0) exit                                                  
      stat4=0                                                            
      do while (stat4==0)                                                
        read (44,100,iostat=stat4) c_time_all_zhd,c_zhd_mm,Tsr_deg,Ps_hpa   
100     format (a12,3x,a12,3x,f6.1,3x,f6.1)                              
        if (stat4/=0) exit                                                
        if (c_time_all_ztd==c_time_all_zhd) then                          
          if( c_zhd_mm(1:1) /= "*") then                                 
            read (c_time_all_ztd,"(f12.7)") time_all_ztd                  
            read (c_zhd_mm,"(f12.7)") zhd_mm                              
            ztd_mm = ztd_m * 1D3                                          
            zwd_mm = ztd_mm - zhd_mm                                      
            write (55,150) time_all_ztd,zwd_mm,Tsr_deg,Ps_hpa,yr,dayofyear,hr
150         format (f12.7,3x,f12.7,3x,f6.1,3x,f6.1,3x,i4,3x,i3.3,3x,i2.2)
            exit                                                         
          end if                                                         
        end if                                                            
      end do                                                              
      rewind(44)                                                         
    end do                                                               
    close(33)                                                            
    close(44)                                                            
    close(55)                                                            
  end do                                                                  
  close(11)                                                               
  close(22)                                                               
end program                                                              


檔案附件如下:

cal_data.rar

620.32 KB, 下载次数: 1

cal_data2.rar

898.4 KB, 下载次数: 1


作者: fcode    时间: 2014-10-30 13:09
是的,这里面的文件读写太多了。读写(IO)都会占用大量的时间。
我的建议是:
1.放弃 list2 文件,只写入 list,然后全部读入内存一个 cfilelist 数组。
2.对 cfilelist 数组进行自我循环。
3.44 号文件只一次读取,读入都内存里。然后循环读取33号文件,对比。
作者: max533    时间: 2014-10-30 13:56
恩恩,感謝您的回覆,不過該怎麼樣修改程式碼才能像你所說這樣呢?!'
因為接觸fortran不久,想說上來問問看各位的意見,順便學習一下。
能否給個範例呢?!或告訴我如何修改呢?!
作者: fcode    时间: 2014-10-30 14:14
替你改不现实,工作量也算挺大的。

你需要学习:数组、循环文件读写、相关知识。
作者: max533    时间: 2014-10-30 15:22
謝謝您提供方向給我去摸索,我先試著去學習看看你說的方式,如果有遇到問題再po上網來跟大家討論。
作者: chiangtp    时间: 2014-11-6 01:09
請試試看, 當可度日如時

test.rar

1.5 KB, 下载次数: 5


作者: chiangtp    时间: 2014-11-6 10:30
抱歉, test.f90程式中, "1.0_r8" 請更正為 "1000.0_r8"
作者: max533    时间: 2014-11-8 19:46
chiangtp 发表于 2014-11-6 10:30
抱歉, test.f90程式中, "1.0_r8" 請更正為 "1000.0_r8"

感謝您提供更快速的方法,讓小弟可以觀摩另類的寫法,感激不盡 ><




欢迎光临 Fortran Coder (http://bbs.fcode.cn/) Powered by Discuz! X3.2