-
Notifications
You must be signed in to change notification settings - Fork 9
How to do line profiling in cython
James Douglass edited this page May 27, 2022
·
3 revisions
pip install line_profiler
- In the cython file itself, add these directives:
# cython: linetrace=True # distutils: define_macros=CYTHON_TRACE_NOGIL=1
- In the cython file, add the
@cython.binding(True)
decorator to the function you want to line profile. For example,@cython.binding(True) def flow_accumulation_d8( flow_dir_raster_path_band, target_flow_accum_raster_path, weight_raster_path_band=None, custom_decay_factor=None
- In
setup.py
, adddefine_macros=[('CYTHON_TRACE', '1')]
to the appropriateExtension()
. - Write a python script to invoke the line profiler. For example:
# doit.py import line_profiler import pygeoprocessing.routing def doit(): profile = line_profiler.LineProfiler( pygeoprocessing.routing.flow_accumulation_d8) profile.runcall( pygeoprocessing.routing.flow_accumulation_d8, (flow_dir_path, 1), target_flow_accum_path, custom_decay_factor=0.5) profile.print_stats()
- Recompile and install
pygeoprocessing
- Execute your python script like so:
kernprof -l doit.py
. - View the line profiling results printed to stdout.