I want to profile a python code which calls fortran routines. I use for this PSTATS, but as shows in the example below, PSTATS does'nt take into account the time spend in fortran routines.
Fortran module (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
Python script which calls the fortran module (test_fortran_pstats.py):
from mod_fortran import *
mod_fortran.sub_sleep(10)
Python script to print profiling statistics (analyse_pstats.py):
import pstats
ps = pstats.Stats('output.pstats')
ps.strip_dirs().sort_stats('tottime').print_stats(5)
Tutorial (compilation and submission):
f2py -c -m mod_fortran mod_fortran.f90
time python -m profile -o output.pstats test_fortran_pstats.py
python analyse_pstats.py
Results:
$> 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 counts only 0.180 seconds while the Fortran routine "sleeps" during 10 seconds.