biogeme.tools.timeit_context_manager module

biogeme.tools.timeit_context_manager.timeit(label, *, logger=None, level=20, threshold_ms=10.0)[source]

Measure the execution time of a block of code and report it only if the elapsed duration exceeds a threshold.

Parameters:
  • label (str) – Name shown in the timing message.

  • logger (Logger | None) – Optional logger used to emit the message. If None, the message is printed to stdout.

  • level (int) – Logging level used when logger is provided.

  • threshold_ms (float) – Minimum duration (in milliseconds) for the timing message to be emitted. Defaults to 10.0.

Return type:

dict[str, Any]

Returns:

A mutable dictionary allowing the user to store values inside the with block (e.g., to capture return values).

Example

# Measure a block without capturing a result
with timeit("matrix inversion"):
    x = np.linalg.inv(A)

# Capture a computed value inside the block
with timeit("simulate choice") as tb:
    tb["value"] = expensive_simulation()

result = tb["value"]
print("Simulation result:", result)