liveBook · Manning

Eager evaluation

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

Eager evaluation is an execution strategy that computes expressions or performs operations immediately rather than deferring them until their results are needed.

Eager evaluation computes an expression when it is specified, producing results immediately rather than deferring work until those results are needed. Pandas and NumPy use this model, while Dask constructs a task graph and computes later. section 10.1.2

Eager execution trades simplicity and repeated access against memory use and unnecessary work:

  • Materializes all results at once, so a list can consume substantially more memory than a generator processing values one at a time. section 2.5.1
  • Computes results that may never be consumed; a lazy computation can stop after finding a single qualifying value. section 2.5.1
  • Makes repeated traversal useful when computation is inexpensive and the materialized representation fits in memory. section 2.5.1
  • Requires measurement rather than assumption: constructing list(range(1000000)) took far longer than creating range(1000000) in the demonstrated benchmark. section 2.2.3

Eagerly materializing a generator

A generator can stream file temperatures with yield, retaining only the current value during iteration. Converting it with temperatures = list(temperatures) eagerly computes and stores the sequence, which helps repeated visits when its memory cost is acceptable. section 2.5.1

import numpy as np
import dask.dataframe as dd                   #1
 
taxes = dd.read_csv("FY2016-STC-Category-Table.csv", sep="\t")
taxes["Amount"] = taxes["Amount"].str.replace(",",
  "").replace("X", np.nan).astype(float)
taxes["Tax_Type"] = taxes["Tax_Type"].astype(
  "category").cat.as_known()                  #2
pivot = taxes.pivot_table(index="Geo_Name",
  columns="Tax_Type", values="Amount")
has_property_info = pivot[
  ~pivot["Property Taxes"].isna()].index      #3
 
pivot_clean = pivot.loc[has_property_info]
frac_property = pivot_clean["Property Taxes"] / pivot_clean["Total Taxes"]

A Dask data-processing pipeline makes the execution boundary visible: transformations build a task graph, and compute() triggers the result. section 10.1.2

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

How deep it goes
Explains execution timing, memory, wasted work, repeated traversal, and measured performance, but does not detail runtime internals.
Examples
Uses range timing, generator-based temperature parsing, list conversion, and a Dask property-tax computation.
What it assumes
Assumes Python sequence handling and introduces generators as the deferred alternative to eager collection creation.
What you can do after
Evaluate eager and lazy alternatives using memory considerations, workload needs, and IPython timing measurements.
How it connects
Connects eager evaluation to lazy evaluation, generators, task graphs, profiling, pandas, and NumPy.

Also explained in

Compare how 5 books explain eager evaluation →

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.