{"id":16402544,"url":"https://github.com/ancestor-mithril/timed-decorator","last_synced_at":"2025-08-29T17:06:40.950Z","repository":{"id":236018856,"uuid":"791737026","full_name":"ancestor-mithril/timed-decorator","owner":"ancestor-mithril","description":"Timing decorator for python functions","archived":false,"fork":false,"pushed_at":"2025-02-04T19:14:55.000Z","size":107,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-07-26T08:07:02.700Z","etag":null,"topics":["python","timing","timing-function"],"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/ancestor-mithril.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-04-25T09:17:38.000Z","updated_at":"2025-05-06T17:27:58.000Z","dependencies_parsed_at":"2024-10-28T09:16:23.469Z","dependency_job_id":"de6266ae-a3f1-4d17-86ad-f2108cf720c1","html_url":"https://github.com/ancestor-mithril/timed-decorator","commit_stats":null,"previous_names":["ancestor-mithril/timed-decorator"],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/ancestor-mithril/timed-decorator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ancestor-mithril%2Ftimed-decorator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ancestor-mithril%2Ftimed-decorator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ancestor-mithril%2Ftimed-decorator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ancestor-mithril%2Ftimed-decorator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ancestor-mithril","download_url":"https://codeload.github.com/ancestor-mithril/timed-decorator/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ancestor-mithril%2Ftimed-decorator/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":272729614,"owners_count":24983531,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-29T02:00:10.610Z","response_time":87,"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":["python","timing","timing-function"],"created_at":"2024-10-11T05:46:31.805Z","updated_at":"2025-08-29T17:06:40.897Z","avatar_url":"https://github.com/ancestor-mithril.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# timed-decorator\n\nSimple and configurable timing decorator with little overhead that can be attached to python functions to measure their execution time.\nCan easily display parameter types and lengths if available and is compatible with NumPy ndarrays, Pandas DataFrames and PyTorch tensors.\n\n\n## Installation\n\n```\npip install --upgrade timed-decorator\n```\n\n## Usage\n\nAttach it to the function you want to time and run the application. \n\n\n```py\nfrom timed_decorator.simple_timed import timed\n\n\n@timed()\ndef fibonacci(n: int) -\u003e int:\n    assert n \u003e 0\n    a, b = 0, 1\n    for _ in range(n):\n        a, b = b, a + b\n    return a\n\n\nfibonacci(10000)\n# fibonacci() -\u003e total time: 1114100ns\n```\n\nFor more advanced usage, consider registering a timed decorator and using it afterward through your codebase. See [Registering a timed decorator](#registering-a-timed-decorator).\n\n### Documentation\n\n1. `timed`\n    * `collect_gc` (`bool`): If `True`, runs a full garbage collection before timing the wrapped function. Default: `True`.\n    * `disable_gc` (`bool`): If `True`, disabled garbage collection during function execution. Default: `False`.\n    * `use_seconds` (`bool`): If `True`, displays the elapsed time in seconds. Default: `False`.\n    * `precision` (`int`): Used in conjunction with `use_seconds`, represents the decimal points used for printing seconds. Default: `9`.\n    * `show_args` (`bool`): If `True`, displays the function arguments according to `display_level`. Useful when timing function calls with arguments of different magnitude. Default: `False`.\n    * `show_kwargs` (`bool`): If `True`, displays the keyword arguments according to `display_level`. Default: `False`.\n    * `display_level` (`int`): The level of verbosity used when printing function arguments ad keyword arguments. If `0`, prints the type of the parameters. If `1`, prints values for all primitive types, shapes for arrays, tensors, dataframes and length for sequences. Otherwise, prints values for all parameters. Default: `1`.\n    * `sep` (`str`): The separator used when printing function arguments and keyword arguments. Default: `', '`.\n    * `stdout` (`bool`): If `True`, writes the elapsed time to stdout. Default: `True`.\n    * `file_path` (`str`): If not `None`, writes the measurement at the end of the given file path. For thread safe file writing configure use `logger_name` instead. Default: `None`.\n    * `logger_name` (`str`): If not `None`, uses the given logger to print the measurement. Can't be used in conjunction with `file_path`. Default: `None`. See [Using a logger](#using-a-logger).\n    * `return_time` (`bool`): If `True`, returns the elapsed time in addition to the wrapped function's return value. Default: `False`.\n    * `out` (`dict`): If not `None`, stores the elapsed time in nanoseconds in the given dict using the fully qualified function name as key, in the following format: (function call counts, total elapsed time, total \"own time\"). If the key already exists, updates the existing value. The elapsed time is equal to \"own time\" for the simple timed decorator. For the nested time decorator, the elapsed time is different from \"own time\" only when another function decorated with a nested timer is called during the execution of the current function. Default: `None`. See [Storing the elapsed time in a dict](#storing-the-elapsed-time-in-a-dict).\n    * `use_qualname` (`bool`): If `True`, If `True`, uses the qualified name of the function when logging the elapsed time. Default: `False`.\n\n2. `nested_timed` is similar to `timed`, however it is designed to work nicely with multiple timed functions that call each other, displaying both the total execution time and the difference after subtracting other timed functions on the same call stack. See [Nested timing decorator](#nested-timing-decorator).\n\n3. `create_timed_decorator` registers a timed decorator with a given name. Can be enabled or disabled during creation.\n   * `name` (`str`): The name of the timed decorator which will be instantiated using the provided arguments. Use this name for retrieving the timed decorator with `timed_decorator.builder.get_timed_decorator`.\n   * `nested` (`bool`): If `True`, uses the `timed_decorator.nested_timed.nested_timed` as decorator, otherwise uses `timed_decorator.simple_timed.timed`. Default: `False`.\n   * `enabled` (`bool`): If `True`, the timed decorator is enabled and used for timing decorated functions. Otherwise, functions decorated with `name` will not be timed. Default: `True`.\n   * Also receives all the other arguments accepted by `timed` and `nested_timed`.\n\n4. `get_timed_decorator` wraps the decorated function and lazily measures its elapsed time using the registered timed decorator. The timer can be registered after the function definition, but must be registered before the first function call. If the timer is disabled, the elapsed time will not be measured.\n\n   * `name` (`str`): The name of the timed decorator registered using `timed_decorator.builder.create_timed_decorator`.\n\n\n### Examples\n\nSimple usage.\n```py\nfrom timed_decorator.simple_timed import timed\n\n\n@timed()\ndef fibonacci(n: int) -\u003e int:\n    assert n \u003e 0\n    a, b = 0, 1\n    for _ in range(n):\n        a, b = b, a + b\n    return a\n\n\nfibonacci(10000)\n# fibonacci() -\u003e total time: 1114100ns\n```\n\nGetting both the function's return value and the elapsed time.\n```py\nfrom timed_decorator.simple_timed import timed\n\n\n@timed(return_time=True)\ndef fibonacci(n: int) -\u003e int:\n    assert n \u003e 0\n    a, b = 0, 1\n    for _ in range(n):\n        a, b = b, a + b\n    return a\n\n\nvalue, elapsed = fibonacci(10000)\nprint(f'10000th fibonacci number has {len(str(value))} digits. Calculating it took {elapsed}ns.')\n# fibonacci() -\u003e total time: 1001200ns\n# 10000th fibonacci number has 2090 digits. Calculating it took 1001200ns.\n```\n\nSet `collect_gc=False` to disable pre-collection of garbage.\n\n```py\nfrom timed_decorator.simple_timed import timed\n\n\n@timed(collect_gc=False)\ndef fibonacci(n: int) -\u003e int:\n    assert n \u003e 0\n    a, b = 0, 1\n    for _ in range(n):\n        a, b = b, a + b\n    return a\n\n\nfibonacci(10000)\n# fibonacci() -\u003e total time: 1062400ns\n```\n\nUsing seconds instead of nanoseconds. \n```py\nfrom timed_decorator.simple_timed import timed\n\n\n@timed(disable_gc=True, use_seconds=True, precision=3)\ndef call_recursive_fibonacci(n: int) -\u003e int:\n    return recursive_fibonacci(n)\n\n\ndef recursive_fibonacci(n: int) -\u003e int:\n    assert n \u003e 0\n    if n \u003e 3:\n        return recursive_fibonacci(n - 1) + recursive_fibonacci(n - 2)\n    if n == 1:\n        return 0\n    return 1\n\n\ncall_recursive_fibonacci(30)\n# call_recursive_fibonacci() -\u003e total time: 0.045s\n```\n\nDisplaying function parameters:\n```py\nfrom timed_decorator.simple_timed import timed\nimport numpy as np\n\n\n@timed(show_args=True, display_level=0)\ndef numpy_operation(array_list, single_array, inplace=False, aggregate='mean', weights=None):\n    x = np.array(array_list)\n    if weights is not None:\n        x = (x.T * weights).T\n\n    if aggregate == 'mean':\n        x = x.mean(axis=0)\n    else:\n        x = x.sum(axis=0)\n\n    if inplace:\n        single_array += x\n        return single_array\n    else:\n        other_array = single_array + x\n        return other_array\n\n\nnumpy_operation(\n    [np.random.rand(2, 3) for _ in range(10)],\n    np.random.rand(2, 3),\n    weights=[i / 10 for i in range(10)],\n    inplace=True\n)\n# numpy_operation(list, ndarray) -\u003e total time: 204200ns\n```\n\nUsing the default display level (1).\n\n```py\nfrom timed_decorator.simple_timed import timed\nimport numpy as np\n\n\n@timed(show_args=True)\ndef numpy_operation(array_list, single_array, inplace=False, aggregate='mean', weights=None):\n    x = np.array(array_list)\n    if weights is not None:\n        x = (x.T * weights).T\n\n    if aggregate == 'mean':\n        x = x.mean(axis=0)\n    else:\n        x = x.sum(axis=0)\n\n    if inplace:\n        single_array += x\n        return single_array\n    else:\n        other_array = single_array + x\n        return other_array\n\n\nnumpy_operation(\n    [np.random.rand(2, 3) for _ in range(10)],\n    np.random.rand(2, 3),\n    weights=[i / 10 for i in range(10)],\n    inplace=True,\n    aggregate='sum'\n)\n# numpy_operation(list(ndarray)[10], ndarray(2, 3)) -\u003e total time: 166400ns\n```\n\nShowing the keyword arguments.\n\n```py\nfrom timed_decorator.simple_timed import timed\nimport numpy as np\n\n\n@timed(show_args=True, show_kwargs=True)\ndef numpy_operation(array_list, single_array, inplace=False, aggregate='mean', weights=None):\n    x = np.array(array_list)\n    if weights is not None:\n        x = (x.T * weights).T\n\n    if aggregate == 'mean':\n        x = x.mean(axis=0)\n    else:\n        x = x.sum(axis=0)\n\n    if inplace:\n        single_array += x\n        return single_array\n    else:\n        other_array = single_array + x\n        return other_array\n\n\nnumpy_operation(\n    [np.random.rand(2, 3) for _ in range(10)],\n    np.random.rand(2, 3),\n    weights=[i / 10 for i in range(10)],\n    inplace=True,\n    aggregate='sum'\n)\n# numpy_operation(list(ndarray)[10], ndarray(2, 3), ('weights', 'list(float)[10]'), ('inplace', 'True'), ('aggregate', 'sum')) -\u003e total time: 166400ns\n```\n\nNot recommended: using display level 2 shows unformatted function arguments.\n\n```py\nfrom timed_decorator.simple_timed import timed\nimport numpy as np\n\n\n@timed(show_args=True, show_kwargs=True, display_level=2)\ndef numpy_operation(array_list, single_array, inplace=False, aggregate='mean', weights=None):\n    x = np.array(array_list)\n    if weights is not None:\n        x = (x.T * weights).T\n\n    if aggregate == 'mean':\n        x = x.mean(axis=0)\n    else:\n        x = x.sum(axis=0)\n\n    if inplace:\n        single_array += x\n        return single_array\n    else:\n        other_array = single_array + x\n        return other_array\n\n\nnumpy_operation(\n    [np.random.rand(1, 3) for _ in range(1)],\n    np.random.rand(1, 3),\n    weights=[i / 10 for i in range(1)],\n    inplace=True\n)\n# numpy_operation([array([[0.74500602, 0.70666224, 0.83888559]])], [[0.74579988 0.51878032 0.06419635]], ('weights', '[0.0]'), ('inplace', 'True')) -\u003e total time: 185300ns\n```\n\nUsing the fully qualified name for timing. \n\n```py\nfrom time import sleep\nfrom timed_decorator.simple_timed import timed\nclass ClassA:\n    @timed(use_qualname=True)\n    def wait(self, x):\n        sleep(x)\nClassA().wait(0.1)\n# ClassA.wait() -\u003e total time: 100943000ns\n```\n\n### Nested timing decorator\n\n```py\nfrom time import sleep\n\nfrom timed_decorator.nested_timed import nested_timed\n\n\n@nested_timed()\ndef nested_fn():\n    @nested_timed()\n    def sleeping_fn(x):\n        sleep(x)\n\n    @nested_timed()\n    def other_fn():\n        sleep(0.5)\n        sleeping_fn(0.5)\n\n    sleep(1)\n    sleeping_fn(1)\n    other_fn()\n    sleeping_fn(1)\n\n\nnested_fn()\n```\nPrints\n```\n        sleeping_fn() -\u003e total time: 1000592700ns, own time: 1000592700ns\n                sleeping_fn() -\u003e total time: 500687200ns, own time: 500687200ns\n        other_fn() -\u003e total time: 1036725800ns, own time: 536038600ns\n        sleeping_fn() -\u003e total time: 1000705600ns, own time: 1000705600ns\nnested_fn() -\u003e total time: 4152634300ns, own time: 1114610200ns\n```\n\n### Using a logger\n```py\nimport logging\nfrom time import sleep\n\nfrom timed_decorator.simple_timed import timed\n\nlogging.basicConfig()\nlogging.root.setLevel(logging.NOTSET)\n\n\n@timed(logger_name='TEST_LOGGER', stdout=False)\ndef fn():\n    sleep(1)\n\n\nfn()\nfn()\n```\nPrints\n```\nINFO:TEST_LOGGER:fn() -\u003e total time: 1000368900ns\nINFO:TEST_LOGGER:fn() -\u003e total time: 1001000200ns\n```\n\nCapture a logger's input\n```py\nimport logging\nfrom io import StringIO\nfrom time import sleep\n\nfrom timed_decorator.simple_timed import timed\n\nlog_stream = StringIO()\nlog_handler = logging.StreamHandler(log_stream)\nlogging.root.setLevel(logging.NOTSET)\nlogging.getLogger('TEST_LOGGER').addHandler(log_handler)\n\n\n@timed(logger_name='TEST_LOGGER', stdout=False)\ndef fn():\n    sleep(1)\n\n\nfn()\nfn()\n\nprint(log_stream.getvalue().split('\\n')[:-1])\n```\nPrints\n```\n['fn() -\u003e total time: 1000214700ns', 'fn() -\u003e total time: 1000157800ns']\n```\n\n### Storing the elapsed time in a dict\n```py\nfrom time import sleep\n\nfrom timed_decorator.simple_timed import timed\n\nns = {}\n\n\n@timed(out=ns, stdout=False)\ndef fn():\n    sleep(1)\n\n\nfn()\nprint(ns)\nfn()\nprint(ns)\n```\nPrints\n```\n{'fn': [1, 1000672000, 1000672000]}\n{'fn': [2, 2001306900, 2001306900]}\n```\n\n### Compatible with PyTorch tensors\n\nSynchronizes cuda device when cuda tensors are passed as function parameters.\n\n```py\nimport torch\nfrom torch import Tensor\n\nfrom timed_decorator.simple_timed import timed\n\n\n@timed(show_args=True)\ndef batched_euclidean_distance(x: Tensor, y: Tensor) -\u003e Tensor:\n    diff = x @ y.T\n    x_squared = (x ** 2).sum(dim=1)\n    y_squared = (b ** 2).sum(dim=1)\n    return x_squared.unsqueeze(-1) + y_squared.unsqueeze(0) - 2 * diff\n\n\na = torch.rand((10000, 800))\nb = torch.rand((12000, 800))\nbatched_euclidean_distance(a, b)\n\nif torch.cuda.is_available():\n    a = a.cuda()\n    b = b.cuda()\n    batched_euclidean_distance(a, b)  # Cuda device is synchronized if function arguments are on device.\n```\nPrints:\n```\nbatched_euclidean_distance(CpuTensor[10000, 800], CpuTensor[12000, 800]) -\u003e total time: 685659400ns\nbatched_euclidean_distance(CudaTensor[10000, 800], CudaTensor[12000, 800]) -\u003e total time: 260411900ns\n```\n\n\n### Registering a timed decorator\n\n```py\nfrom time import sleep\n\nfrom timed_decorator.builder import create_timed_decorator, get_timed_decorator\n\n\n@get_timed_decorator(\"MyCustomTimer\")\ndef main():\n    @get_timed_decorator(\"MyCustomTimer\")\n    def function_1():\n        sleep(0.1)\n\n    @get_timed_decorator(\"MyCustomTimer\")\n    def nested_function():\n        @get_timed_decorator(\"MyCustomTimer\")\n        def function_2():\n            sleep(0.2)\n\n        @get_timed_decorator(\"MyCustomTimer\")\n        def function_3():\n            sleep(0.3)\n\n        function_2()\n        function_2()\n        function_3()\n\n    nested_function()\n    function_1()\n    nested_function()\n    function_1()\n\n\nif __name__ == '__main__':\n    my_measurements = {}\n    create_timed_decorator(\"MyCustomTimer\",\n                           nested=False,  # This is true by default\n                           collect_gc=False,  # I don't want to explicitly collect garbage\n                           disable_gc=True,  # I don't want to wait for garbage collection during measuring\n                           stdout=False,  # I don't wat to print stuff to console\n                           out=my_measurements  # My measurements dict\n                           )\n    main()\n    for key, (counts, elapsed, own_time) in my_measurements.items():\n        print(f'Function {key} was called {counts} time(s) and took {elapsed / 1e+9}s')\n    print()\n\n    # Now I can do stuff with my measurements.\n    functions = sorted(my_measurements.keys(), reverse=True)\n\n    for i in range(len(functions)):\n        fn_1 = functions[i]\n        print(f'Function {fn_1}:')\n        for j in range(i + 1, len(functions)):\n            fn_2 = functions[j]\n            if fn_1.startswith(fn_2):\n                _, elapsed_1, _ = my_measurements[fn_1]\n                _, elapsed_2, _ = my_measurements[fn_2]\n                ratio = elapsed_1 / elapsed_2 * 100\n                print(f'* took {ratio:.2f}% from {fn_2}')\n        print()\n```\n\nPrints:\n```\nFunction main.\u003clocals\u003e.nested_function.\u003clocals\u003e.function_2 was called 4 time(s) and took 0.8019482s\nFunction main.\u003clocals\u003e.nested_function.\u003clocals\u003e.function_3 was called 2 time(s) and took 0.6010157s\nFunction main.\u003clocals\u003e.nested_function was called 2 time(s) and took 1.403365s\nFunction main.\u003clocals\u003e.function_1 was called 2 time(s) and took 0.2007625s\nFunction main was called 1 time(s) and took 1.6043592s\n\nFunction main.\u003clocals\u003e.nested_function.\u003clocals\u003e.function_3:\n* took 42.83% from main.\u003clocals\u003e.nested_function\n* took 37.46% from main\n\nFunction main.\u003clocals\u003e.nested_function.\u003clocals\u003e.function_2:\n* took 57.14% from main.\u003clocals\u003e.nested_function\n* took 49.99% from main\n\nFunction main.\u003clocals\u003e.nested_function:\n* took 87.47% from main\n\nFunction main.\u003clocals\u003e.function_1:\n* took 12.51% from main\n\nFunction main:\n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fancestor-mithril%2Ftimed-decorator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fancestor-mithril%2Ftimed-decorator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fancestor-mithril%2Ftimed-decorator/lists"}