lumopt2.utils.profiler#

Lightweight wall-clock profiling for optimization phases.

This module provides a small, dependency-free profiler that aggregates wall-clock time per named category so users can see where optimization time is spent (FDTD setup, FDTD simulation, dEps/dP computation, data transfer from FDTD, etc.).

The profiler is exposed as a module-level singleton profiler that is enabled by default. Internal lumopt2 modules wrap their key phases with Profiler.measure(), and Profiler.log_summary() is called automatically at the end of every Optimization.run() to log a tree-formatted breakdown.

Notes#

The profiler is single-threaded by design (lumopt2 optimizations are single-threaded). A lock is used for safety but there is no support for concurrent measurements across threads.

Time is measured with time.perf_counter(), which has the highest available resolution. Overhead per measure() call is on the order of 1 microsecond.

Examples#

Disable profiling for a particular optimization run:

>>> import lumopt2 as lmpt
>>> lmpt.profiler.disable()

Print the current summary at any point:

>>> lmpt.profiler.log_summary()

Use the context manager to time a custom block:

>>> with lmpt.profiler.measure("my_custom_phase"):
>>>     do_expensive_work()

Classes#

Profiler([enabled])

Aggregate wall-clock time per named category.