Skip to main content

A simple but robust package to time any function

Project description

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).

Usage

The timen decorator is quite simple to use. it can be imported by simply importing it from the timeN package with

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

@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).

@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...

@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:

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:

@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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

timeN-1.4.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

timeN-1.4-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file timeN-1.4.tar.gz.

File metadata

  • Download URL: timeN-1.4.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.18

File hashes

Hashes for timeN-1.4.tar.gz
Algorithm Hash digest
SHA256 dc524f4940b80da516fb7483cd1fc618bc19deb000c833be224189bf34514bf1
MD5 0bb89f68f53df2020bd115e8d5a1e967
BLAKE2b-256 60a03b82e04a3a70a14dae981dc70225e97e85294743edc18a54880bf9ffe90a

See more details on using hashes here.

File details

Details for the file timeN-1.4-py3-none-any.whl.

File metadata

  • Download URL: timeN-1.4-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.18

File hashes

Hashes for timeN-1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 73c1820e2abdf1ee5aa5b5ee2899b44e103dd0d531f5af850bf237b6e4541ad1
MD5 d7f7ff1c54835836c904d96d9bc3aeef
BLAKE2b-256 50dcd2e17173540060f3d761490f77507ee7d6eacf10ab6bc615c0d53713bc19

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page