Skip to content

How to do line profiling in cython

James Douglass edited this page May 27, 2022 · 3 revisions

Line Profiling in Cython

Setup

  1. pip install line_profiler
  2. In the cython file itself, add these directives:
    # cython: linetrace=True
    # distutils: define_macros=CYTHON_TRACE_NOGIL=1
  3. 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
  4. In setup.py, add define_macros=[('CYTHON_TRACE', '1')] to the appropriate Extension().
  5. 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()

Execution

  1. Recompile and install pygeoprocessing
  2. Execute your python script like so: kernprof -l doit.py .
  3. View the line profiling results printed to stdout.
Clone this wiki locally