Instrumentation adds tracing mechanisms so a profiler can attribute execution time to individual lines. Line-level measurements can expose bottlenecks hidden by whole-function timings, but tracing can slow execution substantially. section 2.2.2 section 5.3.2
To profile line by line:
- Annotate the target function for line_profiler and run it with kernprof, or call a LineProfiler directly. section 2.2.2 section 5.3.2
- For Cython, enable line tracing with linetrace=True and compile with CYTHON_TRACE defined; tracing must be activated at both stages. section 5.3.2
- Treat traced timings as diagnostic: line profiling is expensive, so its absolute runtimes are not comparable to standard profiling. section 5.3.2
import pyximport
import line_profiler #1
import numpy as np
from PIL import Image
pyximport.install(
language_level=3,
setup_args={
'options': {"build_ext":
{"define": 'CYTHON_TRACE'}}, #2
'include_dirs': np.get_include()})
import cyfilter_lprof as cyfilter
image = Image.open("../../04-numpy/aurora.jpg")
gray_filter = Image.open("../filter.png").convert("L")
image_arr, gray_arr = np.array(image), np.array(gray_filter)
profile = line_profiler.LineProfiler(
cyfilter.darken_annotated) #3
profile.runcall(cyfilter.darken_annotated, image_arr, gray_arr)
profile.print_stats()
Example setup that defines CYTHON_TRACE, creates a LineProfiler, profiles a Cython function, and prints statistics. section 5.3.2












