Skip to content

Instantly share code, notes, and snippets.

@mehd-io
Created August 22, 2024 12:31
Show Gist options
  • Save mehd-io/f5df9ecd438366884801c78bf0f264cd to your computer and use it in GitHub Desktop.
Save mehd-io/f5df9ecd438366884801c78bf0f264cd to your computer and use it in GitHub Desktop.
Measure time wrapper
import logging
from datetime import datetime
import functools
# Setting up logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def measure_time(func):
@functools.wraps(func)
def wrapper_measure_time(*args, **kwargs):
start_time = datetime.now()
result = func(*args, **kwargs)
end_time = datetime.now()
elapsed = (end_time - start_time).total_seconds()
logger.info(
f"{func.__name__} function completed in {int(elapsed // 60)} minutes "
f"and {elapsed % 60:.2f} seconds."
)
return result
return wrapper_measure_time
@measure_time
def pipeline():
# Do stuff here, it will be timed
logger.info("Pipeline started.")
if __name__ == "__main__":
pipeline()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment