Metadata-Version: 2.1
Name: context-timing
Version: 0.0.2
Summary: Yet another way to measure code execution duration using a context
License: MIT License
        
        Copyright (c) 2024 patou01
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Project-URL: Homepage, https://github.com/patou01/context-timing
Project-URL: Issues, https://github.com/patou01/context-timing/issues
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Development Status :: 4 - Beta
Requires-Python: >=3
Description-Content-Type: text/markdown
License-File: LICENSE

# context-timing

A python context manager for timing of a block. Somewhat similar to [contexttimer](https://github.com/brouberol/contexttimer) or
[timethis](https://github.com/meribold/timethis).

Uses `perf_counter_ns()` underneath.

The goal of this package is not to be extremely accurate or lightweight. Though both should be acceptable.

# Use case

## Main use case

This package is **not** intended to measure perfomance like timeit would do. The main use case here is to get a quick way to
display how long a block of code takes to execute. For instance if you have a large amount of code that runs, and have a
feeling that the duration of some functions could help you understand the behaviour. This package lets you easily
add a display of this timing.


# How to use

## Basic

```python
from context_timing import measure_time

with measure_time():
    sleep(1)

```

## Set output steam for multiple calls

```python
from context_timing import measure_time, set_log_func

set_log_func(print)

with measure_time():
    sleep(1)

with measure_time():
    sleep(2)
```

## Redirect the output for one measurement

```python
import logging
from context_timing import measure_time, set_log_func

set_log_func(print)

with measure_time(logging.info):
    sleep(1)

with measure_time():
    sleep(2)
```

## In-between timing

The package, similarly to `contexttimer` lets you get the time since start, within the context.

```python
from context_timing import measure_time

with measure_time() as m:
    sleep(1)
    m.print()  # ~1s

```
