When optimizing code, I often need to timeit a part of code or a function.
I am using then the following class
```
import logging
import time
from functools import wraps
from typing import Optional

_logger = logging.getLogger("Timer")

class Timer:
    def __init__(self, description: Optional[str] = None, logger: 
Optional[logging.Logger] = None):
        self.description = "<anonymous code block>" if description is None else 
description
        self.logger = _logger if logger is None else logger
        self.msg = "'%(description)s' ran in %(time).3f seconds"

    def __enter__(self):
        self.start = time.perf_counter()
        return self

    def __exit__(self, *args):
        self.end = time.perf_counter()
        self.interval = self.end - self.start
        self.logger.debug(
            self.msg, {"description": self.description, "time": self.interval}
            # f"Thread: '{get_ident()}'   '{self.description}' ran in 
{round(self.interval, 3)} seconds"
        )

    def __call__(self, f):
        # decorator call
        @wraps(f)
        def wrapper(*args, **kw):
            with Timer(description=f.__qualname__):
                return f(*args, **kw)

        return wrapper
```
that I can use either as a context manager in 
```
with Timer("how long does this take?") as t:
    time.sleep(1)
# output: DEBUG:Timer:'how long does this take?' ran in 1.000 seconds
```
or as a decorator
```
@Timer()
def my_function():
    """this is my function"""
    time.sleep(2)
my_function()
# output: DEBUG:Timer:'my_function' ran in 2.000 seconds
```

This class could be added to the timeit module as it fits well its functional 
scope.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/UY2MS4T64SX4JYTACACEHZD3OGHVRGDT/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to