Ubuntu下Fortran代码性能分析实操指南
一 环境准备与基线计时
program timer_example
implicit none
real :: start_time, end_time, elapsed_time
integer :: i
real :: sum = 0.0
call cpu_time(start_time)
do i = 1, 1000000000
sum = sum + i
end do
call cpu_time(end_time)
elapsed_time = end_time - start_time
print *, "Elapsed time: ", elapsed_time, " seconds"
end program timer_example
编译运行:gfortran -O3 -o timer_example timer_example.f90 && ./timer_example。该方法简单直观,但无法给出函数级热点分布。二 工具链与编译要点
三 分步操作示例
!$OMP PARALLEL DO PRIVATE(i) REDUCTION(+:sum)
do i = 1, 1000000000
sum = sum + i
end do
!$OMP END PARALLEL DO
四 结果解读与优化闭环