我想描述一个调用fortran例程的python代码。 我使用这个PSTATS,但如下例所示,PSTATS确实考虑了在fortran例程中花费的时间。
Fortran模块(mod_fortran.f90):
module mod_fortran
IMPLICIT NONE
CONTAINS
SUBROUTINE sub_sleep(wtime)
INTEGER(kind = 4), intent(in) :: wtime !! in seconds
CHARACTER(len=9) :: cwtime
WRITE(cwtime, 100)wtime
write(*,*) cwtime
CALL system(cwtime)
100 FORMAT('sleep ',I3)
END SUBROUTINE sub_sleep
end module mod_fortran
调用fortran模块的Python脚本(test_fortran_pstats.py):
from mod_fortran import *
mod_fortran.sub_sleep(10)
用于打印分析统计信息的Python脚本(analyse_pstats.py):
import pstats
ps = pstats.Stats('output.pstats')
ps.strip_dirs().sort_stats('tottime').print_stats(5)
教程(编译和提交):
f2py -c -m mod_fortran mod_fortran.f90
time python -m profile -o output.pstats test_fortran_pstats.py
python analyse_pstats.py
结果:
$> time python -m profile -o output.pstats test_fortran_pstats.py
sleep 10
0.236u 0.080s 0:13.58 2.2% 0+0k 47280+240io 1pf+0w
$>python analyse_pstats.py
Fri Apr 3 11:51:02 2015 output.pstats
11170 function calls (10981 primitive calls) in 0.180 seconds
Pstats仅计算0.180秒,而Fortran例程"睡眠" 10秒钟。