shap_enhanced.tools.timer

Timing Utility for Code Execution

Overview

This module provides a lightweight utility class Timer for measuring the execution time of arbitrary blocks of Python code using a context manager (with block). It is intended for profiling and monitoring performance during development, benchmarking, or debugging.

Features

  • Context Manager Interface: Use with Timer(“label”): to automatically time a code block.

  • Human-Readable Output: Prints the elapsed time in seconds with 4-digit precision.

  • Silent Mode: Set verbose=False to suppress output and access .elapsed manually.

Use Case

Useful for timing: - Model training loops. - Data loading or preprocessing steps. - Custom algorithm profiling or benchmarking.

Classes

Timer([label, verbose])

Timing context manager for profiling code execution.

class shap_enhanced.tools.timer.Timer(label='', verbose=True)[source]

Bases: object

Timing context manager for profiling code execution.

This utility measures wall-clock time (in seconds) for any code block wrapped in a with statement. It prints the elapsed time with a label, or stores it for later access via the .elapsed attribute.

Example

with Timer("Sleeping 1s..."):
    time.sleep(1)
Parameters:
  • label (str) – Optional label to describe the timed block.

  • verbose (bool) – If True, prints elapsed time on exit. Otherwise, stores in .elapsed.

Variables:

elapsed (float) – Time in seconds between context entry and exit.