Skip to main content

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

Metadata

Release files for timeN 1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for timeN 1.4
File Size Uploaded Provenance Actions
timeN-1.4.tar.gz 4.5 kB Without provenance Details for timeN-1.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for timeN 1.4
File Interpreter ABI Platform Actions
timeN-1.4-py3-none-any.whl Python 3 none any Details for timeN-1.4-py3-none-any.whl

Total release size: 8.5 kB

Release files / timeN-1.4.tar.gz

Download URL timeN-1.4.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
dc524f4940b80da516fb7483cd1fc618bc19deb000c833be224189bf34514bf1
BLAKE2b-256 checksum
How to use checksums
60a03b82e04a3a70a14dae981dc70225e97e85294743edc18a54880bf9ffe90a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.18

Release files / timeN-1.4-py3-none-any.whl

Download URL timeN-1.4-py3-none-any.whl
Size 4.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
73c1820e2abdf1ee5aa5b5ee2899b44e103dd0d531f5af850bf237b6e4541ad1
BLAKE2b-256 checksum
How to use checksums
50dcd2e17173540060f3d761490f77507ee7d6eacf10ab6bc615c0d53713bc19
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.9.18

Security for timeN 1.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.4 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page