{"id":15907853,"url":"https://github.com/vgilabert94/execution-timer","last_synced_at":"2026-02-26T03:59:10.416Z","repository":{"id":246542014,"uuid":"821438590","full_name":"vgilabert94/execution-timer","owner":"vgilabert94","description":"Provides a decorator to easily measure and optionally save the execution times.","archived":false,"fork":false,"pushed_at":"2024-07-03T12:09:49.000Z","size":20,"stargazers_count":1,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-27T03:41:34.113Z","etag":null,"topics":["decorators-python","execution-time","measure-time","python","timer"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vgilabert94.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-06-28T14:38:05.000Z","updated_at":"2024-10-08T01:36:13.000Z","dependencies_parsed_at":"2024-06-28T15:49:57.279Z","dependency_job_id":"ba256cf8-74ce-4b79-9972-63acc81c296c","html_url":"https://github.com/vgilabert94/execution-timer","commit_stats":{"total_commits":9,"total_committers":2,"mean_commits":4.5,"dds":"0.11111111111111116","last_synced_commit":"b2da2ac5a30747a54e03fc7ae5b6df09684fadba"},"previous_names":["vgilabert94/execution-timer"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/vgilabert94/execution-timer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vgilabert94%2Fexecution-timer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vgilabert94%2Fexecution-timer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vgilabert94%2Fexecution-timer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vgilabert94%2Fexecution-timer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vgilabert94","download_url":"https://codeload.github.com/vgilabert94/execution-timer/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vgilabert94%2Fexecution-timer/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29848657,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-25T22:37:40.667Z","status":"online","status_checked_at":"2026-02-26T02:00:06.774Z","response_time":89,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["decorators-python","execution-time","measure-time","python","timer"],"created_at":"2024-10-06T14:05:24.982Z","updated_at":"2026-02-26T03:59:10.387Z","avatar_url":"https://github.com/vgilabert94.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# ExecutionTimer\n\nExecutionTimer is a utility class for measuring execution times of functions or methods in Python. It provides a **decorator** to easily measure and optionally save the execution times.\n\n## Installation\n\n- Install via pip:\n\n```bash\npip install timer-decorator\n```\n\n- Install via git clone:\n```bash\ngit clone https://github.com/vgilabert94/execution-timer\n```\n\n\n## Documentation\n\n### Function: `time_execution`\n\n#### Parameters\n- `return_measure` (bool, optional): Whether to return the measured time along with the function result (default is False).\n- `nanoseconds` (bool, optional): Whether to use nanoseconds resolution for timing measurements (default is False).\n- `n_iter` (int, optional): The number of iterations to execute the function/method (default is 1). If n_iter \u003e 1 and return_measure=True: result of the function will be the last execution.\n- `return_average` (bool, optional): Whether to return the average time when measuring over multiple iterations (default is True). Only is used when n_iter \u003e 1.\n\n#### Notes\n- If `return_measure`=False and `return_average`=False: a message will be printed for each iteration.\n- If `n_iter` \u003e 1 and `return_measure`=True: the result will be the last function result.\n- If `n_iter` \u003e 1 and `return_measure`=True and `return_average`=False: the result will be the last function result with a list of times for each iteration.\n\n\n### Class: `ExecutionTimer`\n\n#### Parameters\n\n- `save_measure` (bool, optional): Flag to determine if measurement results should be saved (default is True).\n- `nanoseconds` (bool, optional): Flag to use nanoseconds resolution for timing measurements (default is False).\n- `n_iter` : (int, optional): Number of iterations to execute the function/method (default is 1).\n\n#### Notes\n- If `n_iter` \u003e 1 and `return_measure`=True: the result will be the last function result.\n- If `n_iter` \u003e 1 and `return_measure`=False: the time printed will be the last execution result.\n\n\n#### Methods\n\n#### `time_execution`\n\nDecorator method to measure the execution time of a function or method.\n\n- `func` (callable, optional): Function or method to be timed. If None, returns a decorator function.\n- `return_measure` (bool, optional): Flag to indicate if the measured time should be returned along with the function result.\n- `print_measure` (bool, optional): Flag to indicate if the measured time should be printed.\n\nReturns:\n- `wrapper` (callable): Decorated function that measures the execution time of `func`.\n\n#### `get_measured_time`\n\nRetrieve the recorded execution times.\n\nReturns:\n- `dict`: Dictionary containing the measured execution times. Keys are function or method names, and values are lists of measured times.\n\n\n#### `reset_measured_time`\nReset the recorded execution times.\n\n#### `average_measured_time`\nCalculate the average execution times for all recorded functions or methods.\n\nReturns:\n- `dict`: A dictionary where keys are function or class names, and values are the average execution times in seconds. If no times are recorded, the value will be None.\n\n\n## Examples\n\nYou can access a comprehensive examples notebook at the following link: [examples/notebook.ipynb](examples/notebook.ipynb)\n\nLoad packages: \n```python\nimport time\nfrom execution_timer import ExecutionTimer, time_execution\n```\n\n### Example 1: Measuring a function with the default settings.\n\n```python\ntimer = ExecutionTimer()\n\n@timer.time_execution\ndef sample_function(n):\n    time.sleep(n)\n\nprint(sample_function(n=1))\nprint(timer.get_measured_time())\n```\nOutput\n```bash\nNone\n{'sample_function': [1.0000783540003795]}\n```\n\n```python\n@time_execution\ndef sample_function(n):\n    time.sleep(n)\n\nprint(sample_function(n=1))\n```\nOutput\n```bash\nThe 'sample_function' function was executed in 1.00085 seconds.\nNone\n```\n\n### Example 2: Measuring N iterations\n\n```python\ntimer = ExecutionTimer(n_iter=5)\n\n@timer.time_execution\ndef sample_function(n):\n    time.sleep(n)\n\nprint(sample_function(n=1))\nprint(timer.get_measured_time())\nprint(timer.average_measured_time())\n```\nOutput\n```bash\nThe 'sample_function' function was executed in 1.00098 seconds.\nNone\n{'sample_function': [1.0010039510007118, 1.001013477000015, 1.001078371999938, 1.0008607439995103, 1.0009819289998632]}\n{'sample_function': 1.0009876946000076}\n```\n\n```python\n@time_execution(n_iter=5)\ndef sample_function(n):\n    time.sleep(n)\n\nprint(sample_function(n=1))\n```\nOutput\n```bash\nThe 'sample_function' function was executed in 1.00030 seconds.\nNone\n```\n\n### Example 8: Measuring a Method from a class\n\n```python\ntimer = ExecutionTimer()\n\nclass SampleClass:\n    def __init__(self):\n        pass\n\n    @timer.time_execution(return_measure=True)\n    def sample_method(self, n):\n        time.sleep(n)\n\n    @timer.time_execution\n    def sample_method_x2(self, n):\n        time.sleep(2*n)\n\nsample = SampleClass()\nprint(sample.sample_method(n=1))\nprint(sample.sample_method_x2(n=1))\nprint(timer.get_measured_time())\n```\nOutput\n```bash\n(None, 1.0002362490004089)\nThe 'sample_method_x2' function was executed in 2.00203 seconds.\nNone\n{'SampleClass': {'sample_method': [1.0002362490004089], 'sample_method_x2': [2.0020319710001786]}}\n```\n\n## LICENSE \n\nDistributed under the MIT License. See LICENSE.txt for more information.\n\n\n## Contact\n\n[Vicent Gilabert](mailto:gilabert_vicent@hotmail.com)\n\n[Linkedin](https://www.linkedin.com/in/vgilabert/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvgilabert94%2Fexecution-timer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvgilabert94%2Fexecution-timer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvgilabert94%2Fexecution-timer/lists"}