Created
December 4, 2024 12:12
-
-
Save OlegKorn/8dde23a7f88f8f1f4706dec4a40242e0 to your computer and use it in GitHub Desktop.
python - measure execution time function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# author - https://github.com/s3rgeym | |
import time | |
import functools | |
import typing | |
def timeit(fn: typing.Callable) -> typing.Callable: | |
@functools.wraps(fn) | |
def timed(*args: typing.Any, **kwargs: typing.Any) -> typing.Any: | |
try: | |
dt = -time.monotonic() | |
return fn(*args, **kwargs) | |
finally: | |
dt += time.monotonic() | |
print("function %s tooks %.3fs", fn.__name__, dt) | |
return timed | |
# example | |
@timeit | |
def delete_forbidden_chars(string): | |
# deleting forbidden chars | |
FORBIDDEN_CHARS = re.escape(punctuation) | |
cleared_string = re.sub('['+FORBIDDEN_CHARS+']', '', string).replace('"', "") | |
return cleared_string |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment