Skip to main content

code_timer

Simple python timer to use for single lines of code, or entire functions/classes

Latest version Python versions CircleCI

New Features!

  • Use code_timer.Timer as a class, context manager, decorator
    • Offers the most versatility and flexibility for your needs
  • Use separate code_timer.timer decorator
  • Use code_timer.timeit decorator to measure the time to run the same function multiple times
    • Useful when comparing the efficiency of one runtime vs another
    • By default, will run the function 10,000 times. The number of runs is configurable
    • DO NOT USE THIS FOR RECURSIVE FUNCTIONS!!
  • Logger allowing you to set the streaming level or hide the logging entirely

Basic Usage

code_timer.Timer

  1. As a class:

    t = Timer(name="class")
    t.start()
    # Do something
    t.stop()
    
  2. As a context manager:

  • Useful when trying to time a recursive function

    with Timer(name="context manager") as t:
        # Do something
    print(f"Elapsed time: {t.elapsed_time}")
    
  1. As a decorator:

    @Timer(name="decorator")
    def stuff():
        # Do something
    

code_timer.timer

  1. Without braces

    @timer
    def your_func():
        # Do something
    
  2. With braces

    @timer(name="my_timer")
    def your_func():
        # Do something
    

code_timer.timeit

  • Time is reported at logging level: logging.INFO
  • The following assumes the standard formatting for the code_timer logger
  1. Default

    • will repeat the function 10,000 times and report the fastest three runs
    @timeit
    def your_func():
        # Do something
    
    • Output: 2020-03-12 21:56:46: code_timer.INFO - Best 3 of 10000 for your_func: 0.2200 ms; 0.2220 ms; 0.2232 ms
  2. Pass in the number of times to repeat

    @timeit(num_repeats=50)
    def your_func():
        # Do something
    
    • Output: 2020-03-12 21:56:46: code_timer.INFO - Best 3 of 50 for your_func: 0.2200 ms; 0.2220 ms; 0.2232 ms

Download files

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

Source Distribution

code_timer-2.0.0.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

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

code_timer-2.0.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file code_timer-2.0.0.tar.gz.

File metadata

  • Download URL: code_timer-2.0.0.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.11

File hashes

Hashes for code_timer-2.0.0.tar.gz
Algorithm Hash digest
SHA256 4fdc1cd4cef038d5eae00d3edaecabf2130ee936e76bf6065f36c6bc70be9a2d
MD5 71f387b02f73113539116c6b979b0ccc
BLAKE2b-256 d6756696858c0f13c2dbb769e9278dd20c4ee39dc923589ebebb83549f104ec7

See more details on using hashes here.

File details

Details for the file code_timer-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: code_timer-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.2 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.0 CPython/3.8.11

File hashes

Hashes for code_timer-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 61b39f732196b729b90ded689d76067963d4a0611fc5d50988c9cdfbbdc1d5d2
MD5 0774bd6007a8645feb3a44f601dd08d1
BLAKE2b-256 9f7f31847cb8467cba607634b6fe53270b7e9acc121f056d13ba7eb7437f9042

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

2.0.0 This release

2 files

1.1.7

2 files

1.1.4

1 file

1.1.3

1 file

1.1.2

1 file

1.1.1

1 file

1.1.0

1 file

1.0.4

1 file

1.0.3

1 file

v1.0.1

1 file

1.0.0

1 file

Supported by

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