Skip to main content

Hierarchical timing utility with nested context manager support

Project description

muTimer

muTimer is a hierarchical timing utility with nested context manager support. It provides fine-grained timing of code sections.

Features

  • Nested timing contexts that track parent-child relationships
  • Accumulation of time across multiple calls to the same timer
  • Call counting for repeated operations
  • Hierarchical summary output in tabular format
  • Optional depth limiting for nested timers
  • MPI-aware summaries (single output on rank 0 with min/mean/max across ranks)

Usage

from muTimer import Timer

timer = Timer()

with timer("outer"):
    # some code
    with timer("inner"):
        # nested code
    with timer("inner"):  # called again - time accumulates
        # more nested code

timer.print_summary()

Output:

==============================================================================
Timing Summary
==============================================================================
Name                                  Total    Calls      Average   % Parent
------------------------------ ------------ -------- ------------ ----------
outer                              22.55 ms        1            -          -
  inner                            12.50 ms        2      6.25 ms      55.4%
  (other)                          10.06 ms        -            -      44.6%
==============================================================================

MPI Support

When running under MPI, pass a communicator to avoid every rank printing its own summary. Both mpi4py communicators and muGrid communicators are accepted.

from mpi4py import MPI
from muTimer import Timer

timer = Timer(comm=MPI.COMM_WORLD)

with timer("outer"):
    # some code
    pass

timer.print_summary()  # printed once, on rank 0

Timing data is gathered to rank 0, which prints a single summary showing the mean, minimum and maximum time spent in each section across all ranks:

===========================================================================================
Timing Summary (4 MPI processes)
===========================================================================================
Name                                   Mean          Min          Max    Calls   % Parent
------------------------------ ------------ ------------ ------------ -------- ----------
outer                              23.30 ms     22.55 ms     24.05 ms        1          -
  inner                            12.95 ms     12.50 ms     13.40 ms        2      55.6%
  (other)                          10.35 ms            -            -        -      44.4%
===========================================================================================

If the communicator cannot gather Python objects (no underlying mpi4py communicator), the summary is still printed only on rank 0, using that rank's local timings.

muTimer has no dependency on mpi4py (or any other MPI package): the communicator is duck-typed. Anything that provides the mpi4py communicator interface works, e.g. NuMPI's MPI module, which falls back to a serial stub when mpi4py is not installed:

from NuMPI import MPI  # mpi4py if installed, serial stub otherwise
from muTimer import Timer

timer = Timer(comm=MPI.COMM_WORLD)

Memory Tracking

You can also track memory usage (Resident Set Size) by enabling track_memory=True. This requires the psutil package.

import time
from muTimer import Timer

# Create a timer with memory tracking enabled
timer = Timer(track_memory=True)

with timer("outer"):
    # allocate some memory
    large_list = [0] * 1000000
    time.sleep(0.01)
    
    with timer("inner"):
        another_list = [1] * 2000000
        time.sleep(0.01)

    with timer("inner"):
        more_memory = [2] * 500000
        time.sleep(0.005)

timer.print_summary()

Output:

============================================================================================
Timing Summary
============================================================================================
Name                                  Total    Calls      Average   % Parent       Memory
------------------------------ ------------ -------- ------------ ---------- ------------
outer                              33.74 ms        1            -          -     26.78 MB
  inner                            20.52 ms        2     10.26 ms      60.8%     19.12 MB
  (other)                          13.23 ms        -            -      39.2%      7.66 MB
============================================================================================

License

muTimer is distributed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mutimer-1.1.0.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mutimer-1.1.0-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file mutimer-1.1.0.tar.gz.

File metadata

  • Download URL: mutimer-1.1.0.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mutimer-1.1.0.tar.gz
Algorithm Hash digest
SHA256 d38a2014cca5e133672ed6ac36790c8549c83aff2339cfea73efb23e0370a613
MD5 d8f212c67a1e4ec8bc367e4cd47be8a8
BLAKE2b-256 b6f3fb3cc4a64e82d89a88ef6f5101432420dc0d14a7efa8a8e6543915a0398a

See more details on using hashes here.

File details

Details for the file mutimer-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: mutimer-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mutimer-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bf057715b74508df6fbee5478d265554b9d347d6d01d20b74aa5c0dee1175ec5
MD5 48ba0aa0e78ff6e7ddddea74c9e4d084
BLAKE2b-256 575de014f4110a9d7cbe0625e9d4b25d29ac27d420d5472ada69dc43eb8a4e5b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page