timing

Provides Matlab-like tic, tac and toc functions.

class quantecon.util.timing.Timer(message='', precision=None, unit='seconds', verbose=True)[source]

Bases: object

A context manager for timing code execution.

This provides a modern context manager approach to timing, allowing patterns like with Timer(): instead of manual tic/toc calls.

Parameters:
messagestr, optional(default=””)

Custom message to display with timing results.

precisionint, optional(default=None)

Number of decimal places to display for seconds. If None, uses the global default precision from quantecon.timings.

unitstr, optional(default=”seconds”)

Unit to display timing in. Options: “seconds”, “milliseconds”, “microseconds”

verbosebool, optional(default=True)

If True, print timing results. If False, suppress printing of timing results.

Attributes:
elapsedfloat

The elapsed time in seconds. Available after exiting the context.

Examples

Basic usage: >>> with Timer(): … # some code … pass 0.0000 seconds elapsed

With custom message and precision: >>> with Timer(“Computing results”, precision=6): … # some code … pass Computing results: 0.000001 seconds elapsed

Store elapsed time for comparison: >>> timer = Timer(verbose=False) >>> with timer: … # some code … pass >>> print(f”Method took {timer.elapsed:.6f} seconds”) Method took 0.000123 seconds

quantecon.util.timing.loop_timer(n, function, args=None, verbose=True, digits=2, best_of=3)[source]

Return and print the total and average time elapsed for n runs of function.

Parameters:
nscalar(int)

Number of runs.

functionfunction

Function to be timed.

argslist, optional(default=None)

Arguments of the function.

verbosebool, optional(default=True)

If True, then prints average time.

digitsscalar(int), optional(default=2)

Number of digits printed for time elapsed.

best_ofscalar(int), optional(default=3)

Average time over best_of runs.

Returns:
average_timescalar(float)

Average time elapsed for n runs of function.

average_of_bestscalar(float)

Average of best_of times for n runs of function.

quantecon.util.timing.tac(verbose=True, digits=2)[source]

Return and print elapsed time since last tic(), tac(), or toc().

Parameters:
verbosebool, optional(default=True)

If True, then prints time.

digitsscalar(int), optional(default=2)

Number of digits printed for time elapsed.

Returns:
elapsedscalar(float)

Time elapsed since last tic(), tac(), or toc().

quantecon.util.timing.tic()[source]

Save time for future use with tac() or toc().

Returns:
None

This function doesn’t return a value.

quantecon.util.timing.timeit(func, runs=1, stats_only=False, verbose=True, results=False, **timer_kwargs)[source]

Execute a function multiple times and collect timing statistics.

This function provides a convenient way to time a function multiple times and get summary statistics, using the Timer context manager internally.

Parameters:
funccallable

Function to execute multiple times. Function should take no arguments, or be a partial function or lambda with arguments already bound.

runsint, optional(default=1)

Number of runs to execute. Must be a positive integer.

stats_onlybool, optional(default=False)

If True, only display summary statistics. If False, display individual run times followed by summary statistics.

verbosebool, optional(default=True)

If True, print nicely formatted timing output all at once at the end. If False, suppress all output.

resultsbool, optional(default=False)

If True, return dictionary with timing results. If False, return None.

**timer_kwargs

Keyword arguments to pass to Timer (message, precision, unit, verbose).

Returns:
dict or None

If results=True, returns dictionary containing timing results with keys: - ‘elapsed’: list of elapsed times for each run - ‘average’: average elapsed time - ‘minimum’: minimum elapsed time - ‘maximum’: maximum elapsed time If results=False, returns None.

Examples

Basic usage: >>> def slow_function(): … time.sleep(0.01) >>> timeit(slow_function, runs=3) Run 1: 0.01 seconds Run 2: 0.01 seconds Run 3: 0.01 seconds Average: 0.01 seconds, Minimum: 0.01 seconds, Maximum: 0.01 seconds

Summary only: >>> timeit(slow_function, runs=3, stats_only=True) Average: 0.01 seconds, Minimum: 0.01 seconds, Maximum: 0.01 seconds

With custom Timer options: >>> timeit(slow_function, runs=2, unit=”milliseconds”, precision=1) Run 1: 10.1 ms Run 2: 10.0 ms Average: 10.1 ms, Minimum: 10.0 ms, Maximum: 10.1 ms

Return results for further analysis: >>> results = timeit(slow_function, runs=2, results=True) >>> print(f”Average time: {results[‘average’]:.4f} seconds”)

Quiet mode: >>> timeit(slow_function, runs=2, verbose=False) # No output

With function arguments using lambda: >>> add_func = lambda: expensive_computation(5, 10) >>> timeit(add_func, runs=2)

quantecon.util.timing.toc(verbose=True, digits=2)[source]

Return and print time elapsed since last tic().

Parameters:
verbosebool, optional(default=True)

If True, then prints time.

digitsscalar(int), optional(default=2)

Number of digits printed for time elapsed.

Returns:
elapsedscalar(float)

Time elapsed since last tic().