Original code copyright (C) 2009-2022 Rudolf Cardinal (

This file is part of cardinal_pythonlib.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Timers for performance tuning.

class cardinal_pythonlib.timing.MultiTimer(start: bool = True)[source]

Mutually exclusive timing of a set of events.

Maintains a start count and times for a set of named timers.

See example in MultiTimerContext.


start – start the timer immediately?

report() None[source]

Finish and report to the log.

reset() None[source]

Reset the timers.

set_timing(timing: bool, reset: bool = False) None[source]

Manually set the timing parameter, and optionally reset the timers.

  • timing – should we be timing?

  • reset – reset the timers?

start(name: str, increment_count: bool = True) None[source]

Start a named timer.

  • name – name of the timer

  • increment_count – increment the start count for this timer

stop(name: str) None[source]

Stop a named timer.


name – timer to stop

class cardinal_pythonlib.timing.MultiTimerContext(multitimer: MultiTimer, name: str)[source]

Context manager for MultiTimer.


import logging
from time import sleep
from cardinal_pythonlib.timing import MultiTimer, MultiTimerContext

timer = MultiTimer(start=True)

def f2():
    with MultiTimerContext(timer, "f2"):
        print("starting f2")
        print("finishing f2")

def f1():
    with MultiTimerContext(timer, "f1"):
        print("starting f1")
        print("finishing f1")

  • multitimerMultiTimer to use

  • name – name of timer to start as we enter, and stop as we exit