timeN


NametimeN JSON
Version 1.4 PyPI version JSON
download
home_pageNone
SummaryA simple but robust package to time any function
upload_time2024-03-20 03:15:20
maintainerNone
docs_urlNone
authorNone
requires_python>=3.7
licenseCopyright (c) 2024 Zack Darling Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords timer timing decorator development
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Package Description

timeN is a simple package that contains a customizable `@timen` decorator which times how long it takes (in milliseconds) for the decorated function to run n times. The decorator supports printing, forwards return values to the calling function, and can even work on recursive functions (see [A Note about Recursion](#a-note-about-recursion)).

# Usage

The timen decorator is quite simple to use. it can be imported by simply importing it from the timeN package with 
```python
from timeN import timeN
```
From there, the timeN decorator can be used like any other decorator, with the exception that you must end the decorator in parentheses since it takes optional parameters
```python
@timeN()
def foo():
    print("Hello World")
```
Output:
```
Hello World
...
Hello World
foo took 2.0 milliseconds to run 10 times
```
The 2 optional parameters are: n (number of times to run the function) and round_to (number decimal places to round the final result to).
```python
@timeN(100, 5)
def foo():
    print("Hello World")
```
Output:
```
Hello World
...
Hello World
foo took 11.99365 milliseconds to run 100 times
```

# A Note about Recursion

Due to limitations within python decorators themselves, if you would like to time a recursive function like the following...
```python
@timeN(1, 10)
def virfib(n):
  if n == 0:
    return 0
  elif n == 1:
    return 1
  else:
    return virfib(n-2) + virfib(n-1)

virfib(4)
```
...then that will not work. The reason why is because every time virfib is recursively called, the decorator function is also called. This leads to recursive calling of the timing function, meaning that a function that is recursively called 4 times will be timed 5 times (once for the original function call and once more for EACH recursive call).

To fix this issue, simplay call your recursive function from another function, and time the second function instead:
```python
def virfib(n):
  if n == 0:
    return 0
  elif n == 1:
    return 1
  else:
    return virfib(n-2) + virfib(n-1)

@timeN(1, 10)
def virfib_caller():
    print(virfib(4))

virfibcaller()
```
In this way only the call to `virfib_caller()` is timed, and since all that that function does is call `virfib`, it will give the desired result.

A higher order recursive function will work flawlessly with timeN:
```python
@timeN(1, 10)
def virfib(n):
    def fib_helper(i, curr, next):
        return curr if i >= n else fib_helper(i+1, next, curr+next)
    return fib_helper(0, 0, 1)

virfib(100)
```
Output:
```
-------------- Captured return value from function virfib --------------
354224848179261915075
virfib took 0.0 milliseconds to run 1 times
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "timeN",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": "timer, timing, decorator, development",
    "author": null,
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/60/a0/3b82e04a3a70a14dae981dc70225e97e85294743edc18a54880bf9ffe90a/timeN-1.4.tar.gz",
    "platform": null,
    "description": "# Package Description\n\ntimeN is a simple package that contains a customizable `@timen` decorator which times how long it takes (in milliseconds) for the decorated function to run n times. The decorator supports printing, forwards return values to the calling function, and can even work on recursive functions (see [A Note about Recursion](#a-note-about-recursion)).\n\n# Usage\n\nThe timen decorator is quite simple to use. it can be imported by simply importing it from the timeN package with \n```python\nfrom timeN import timeN\n```\nFrom there, the timeN decorator can be used like any other decorator, with the exception that you must end the decorator in parentheses since it takes optional parameters\n```python\n@timeN()\ndef foo():\n    print(\"Hello World\")\n```\nOutput:\n```\nHello World\n...\nHello World\nfoo took 2.0 milliseconds to run 10 times\n```\nThe 2 optional parameters are: n (number of times to run the function) and round_to (number decimal places to round the final result to).\n```python\n@timeN(100, 5)\ndef foo():\n    print(\"Hello World\")\n```\nOutput:\n```\nHello World\n...\nHello World\nfoo took 11.99365 milliseconds to run 100 times\n```\n\n# A Note about Recursion\n\nDue to limitations within python decorators themselves, if you would like to time a recursive function like the following...\n```python\n@timeN(1, 10)\ndef virfib(n):\n  if n == 0:\n    return 0\n  elif n == 1:\n    return 1\n  else:\n    return virfib(n-2) + virfib(n-1)\n\nvirfib(4)\n```\n...then that will not work. The reason why is because every time virfib is recursively called, the decorator function is also called. This leads to recursive calling of the timing function, meaning that a function that is recursively called 4 times will be timed 5 times (once for the original function call and once more for EACH recursive call).\n\nTo fix this issue, simplay call your recursive function from another function, and time the second function instead:\n```python\ndef virfib(n):\n  if n == 0:\n    return 0\n  elif n == 1:\n    return 1\n  else:\n    return virfib(n-2) + virfib(n-1)\n\n@timeN(1, 10)\ndef virfib_caller():\n    print(virfib(4))\n\nvirfibcaller()\n```\nIn this way only the call to `virfib_caller()` is timed, and since all that that function does is call `virfib`, it will give the desired result.\n\nA higher order recursive function will work flawlessly with timeN:\n```python\n@timeN(1, 10)\ndef virfib(n):\n    def fib_helper(i, curr, next):\n        return curr if i >= n else fib_helper(i+1, next, curr+next)\n    return fib_helper(0, 0, 1)\n\nvirfib(100)\n```\nOutput:\n```\n-------------- Captured return value from function virfib --------------\n354224848179261915075\nvirfib took 0.0 milliseconds to run 1 times\n```\n",
    "bugtrack_url": null,
    "license": "Copyright (c) 2024 Zack Darling  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
    "summary": "A simple but robust package to time any function",
    "version": "1.4",
    "project_urls": null,
    "split_keywords": [
        "timer",
        " timing",
        " decorator",
        " development"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "50dcd2e17173540060f3d761490f77507ee7d6eacf10ab6bc615c0d53713bc19",
                "md5": "d7f7ff1c54835836c904d96d9bc3aeef",
                "sha256": "73c1820e2abdf1ee5aa5b5ee2899b44e103dd0d531f5af850bf237b6e4541ad1"
            },
            "downloads": -1,
            "filename": "timeN-1.4-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d7f7ff1c54835836c904d96d9bc3aeef",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 4030,
            "upload_time": "2024-03-20T03:15:19",
            "upload_time_iso_8601": "2024-03-20T03:15:19.195788Z",
            "url": "https://files.pythonhosted.org/packages/50/dc/d2e17173540060f3d761490f77507ee7d6eacf10ab6bc615c0d53713bc19/timeN-1.4-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "60a03b82e04a3a70a14dae981dc70225e97e85294743edc18a54880bf9ffe90a",
                "md5": "0bb89f68f53df2020bd115e8d5a1e967",
                "sha256": "dc524f4940b80da516fb7483cd1fc618bc19deb000c833be224189bf34514bf1"
            },
            "downloads": -1,
            "filename": "timeN-1.4.tar.gz",
            "has_sig": false,
            "md5_digest": "0bb89f68f53df2020bd115e8d5a1e967",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 4497,
            "upload_time": "2024-03-20T03:15:20",
            "upload_time_iso_8601": "2024-03-20T03:15:20.173043Z",
            "url": "https://files.pythonhosted.org/packages/60/a0/3b82e04a3a70a14dae981dc70225e97e85294743edc18a54880bf9ffe90a/timeN-1.4.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-20 03:15:20",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "timen"
}
        
Elapsed time: 0.20510s