liveBook · Manning

Instrumentation

As explained in Fast Python: High performance techniques for large datasets

Instrumentation is the addition or activation of mechanisms in software or systems to observe, measure, or record their behavior during execution.

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

How Fast Python: High performance techniques for large datasets covers it

How deep it goes
Configures Python and Cython line tracing, but defers the underlying C macros and Python build infrastructure.
Examples
Profiles a get_distance function and a Cython image-darkening function to inspect line costs.
What it assumes
Assumes familiarity with Python functions; Cython profiling additionally requires configuring the tracing directive and compiler macro.
What you can do after
Diagnose/debug performance bottlenecks by locating costly source lines, while accounting for tracing overhead.
How it connects
Connects line_profiler with Cython tracing, Python profiling, and performance optimization.

Also explained in

Compare how 13 books explain instrumentation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fast Python: High performance techniques for large datasets. The section links point to the parts of the book it draws on.