Skip to content

Instantly share code, notes, and snippets.

@OlegKorn
Created December 4, 2024 12:12
Show Gist options
  • Save OlegKorn/8dde23a7f88f8f1f4706dec4a40242e0 to your computer and use it in GitHub Desktop.
Save OlegKorn/8dde23a7f88f8f1f4706dec4a40242e0 to your computer and use it in GitHub Desktop.
python - measure execution time function
# 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