2016-05-13 138 views
-1

我需要對fortran程序的一部分進行基準測試,以瞭解和量化特定更改的影響(爲了使代碼更易維護,我們希望使代碼更加面向對象,充分利用函數指針例)。基準測試fortran循環

我有一個循環調用幾次相同的子程序來執行有限元素的計算。我想看看使用函數指針而不是硬編碼函數的影響。

do i=1,n_of_finite_elements 
    ! Need to benchmark execution time of this code 
end do 

什麼是一個簡單的方法來獲得這種循環的執行時間,並以一種很好的方式格式化?

+0

你在尋找的東西,型材的需要作爲一個整體多久的代碼,或只是粗略時間?你使用的是什麼操作系統和編譯器?查看['CPU_TIME'](https://gcc.gnu.org/onlinedocs/gfortran/CPU_005fTIME.html)的簡單方法來手動檢查。另外[這個問題](http://scicomp.stackexchange.com/questions/6812/fortran-best-way-to-time-sections-of-your-code)可能會有用。 – Gabe

+1

@加貝請不要使用'CPU_TIME'。它並不適合時間測量,特別是在[並行編程](https://stackoverflow.com/questions/25465101/fortran-parallel-programming)中。相反,使用'SYSTEM_CLOCK'。 –

+0

作爲一個整體的時機,但基準是(我希望)不僅僅是注意系統時間。例如,[ruby基準](http://ruby-doc.org/stdlib-1.9.3/libdoc/benchmark/rdoc/Benchmark.html)提供了一些有趣的指標,如用戶,系統和總時間,在非常很好的顯示,甚至解釋了基準測試時需要採取的預防措施。 –

回答

2

我有一個測量通過各種陣列的https://github.com/jlokimlin/array_passing_performance.git

表現它使用https://github.com/jlokimlin/cpu_timer.git的CpuTimer派生數據類型的GitHub的項目。

用法:

use, intrinsic :: iso_fortran_env, only: & 
    wp => REAL64, & 
    ip => INT32 

use type_CpuTimer, only: & 
    CpuTimer 

type (CpuTimer) :: timer 
real (wp)  :: wall_clock_time 
real (wp)  :: total_processor_time 
integer (ip) :: units = 0 ! (optional argument) = 0 for seconds, or 1 for minutes, or 2 for hours 

! Starting the timer 
call timer%start() 

do i = 1, n 

    !...some big calculation... 

end do 

! Stopping the timer 
call timer%stop() 

! Reading the time 
wall_clock_time = timer%get_elapsed_time(units) 

total_processor_time = timer%get_total_cpu_time(units) 

! Write time stamp to standard output 
call timer%print_time_stamp() 

! Write compiler info to standard output 
call timer%print_compiler_info() 
+0

這很有趣,可以很好用,但很難涵蓋整個話題。 –

+0

那實際上是這樣的我想要的東西。我會盡力限制我的問題,使其與我猜想的答案範圍相匹配。 –

+0

在這種情況下,您可以始終使用一組標準內部過程'system_clock()'和'cpu_time()',儘管此包在一個對象中具有更多功能。 –