Skip to content

_perf

Lightweight Python-side perf instrumentation.

Emits [PERF:py] checkpoints through the standard pyRevit logger at DEBUG level so Python startup-script timings interleave with the C# loader's [PERF] timeline in the regular runtime log / output window.

Self-contained on purpose: imports only stdlib at module load so it can be the first line of pyrevit/init.py without triggering circular loads. The pyRevit logger is resolved lazily on first use; checkpoints that fire before the logger is importable (early bootstrap) are silently skipped.

Classes

time_block(label)

Bases: object

Context manager: time a block independently of the running timeline.

Emits one DEBUG [PERF:py] line at exit, indented two extra spaces past mark() lines to mirror C# sub-item indentation ([PERF] <name>:).

Source code in pyrevitlib/pyrevit/_perf.py
def __init__(self, label):
    self.label = label
    self._t0 = None

Attributes

label = label instance-attribute

Functions

mark(label)

Record a perf checkpoint.

Emits one DEBUG [PERF:py] line with elapsed time since the previous mark. Format mirrors the C# [PERF] lines for visual parity. No-op until the pyRevit logger is importable.

Source code in pyrevitlib/pyrevit/_perf.py
def mark(label):
    """Record a perf checkpoint.

    Emits one DEBUG `[PERF:py]` line with elapsed time since the previous
    mark. Format mirrors the C# `[PERF]` lines for visual parity. No-op until
    the pyRevit logger is importable.
    """
    now = _now()
    delta_ms = (now - _LAST[0]) * 1000.0
    _LAST[0] = now
    lg = _logger()
    if lg is None:
        return
    try:
        lg.debug("[PERF:py] %s: %.0fms", label, delta_ms)
    except Exception:
        pass