Skip to main content

A class for timing code (start/stop, context manager, decorator).

Project description

A class for timing code. A Stopwatch object can be used to time code using its start and stop methods:

from et_stopwatch import Stopwatch

stopwatch = Stopwatch() # create and start the stopwatch
sleep(1)
stopwatch.stop()
print(stopwatch)

stopwatch : 1.003744 s

Use as a context manager:

with Stopwatch(message='This took') as sw: # using a custom message
    for i in range(3):
        sleep(1)
        print(i, sw.stop(), 's') # stop() returns the time since the last call to start|stop in seconds

0 1.004943
1 1.004948
2 1.003404
This took :
    total  : 3.013295 s
    minimum: 1.003404 s
    maximum: 1.004948 s
    mean   : 1.004432 s
    stddev : 0.000727 s
    count  : 3

Since stop was called more than once, some statistics are printed.

Use as a decorator:

@Stopwatch(name="say_hi_and_sleep_two_seconds", ndigits=3) # custom message, print only 3 digits.
def say_hi_and_sleep_two_seconds():
    print("hi")
    sleep(2)

say_hi_and_sleep_two_seconds()

hi
say_hi_and_sleep_two_seconds : 2.003 s

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

et_stopwatch-1.2.2.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

et_stopwatch-1.2.2-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file et_stopwatch-1.2.2.tar.gz.

File metadata

  • Download URL: et_stopwatch-1.2.2.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.9.5 Darwin/22.2.0

File hashes

Hashes for et_stopwatch-1.2.2.tar.gz
Algorithm Hash digest
SHA256 aa86dfce27e01ac0c79ff122ac25bf35a36d22bb0806c73f528dbf2fce5d89b4
MD5 71a0eaca5e4d7fc561f7e18ab727b642
BLAKE2b-256 668460ecc5235357e2d96c6530b1a99716d935f56ae31453530f7de115f2bb1d

See more details on using hashes here.

File details

Details for the file et_stopwatch-1.2.2-py3-none-any.whl.

File metadata

  • Download URL: et_stopwatch-1.2.2-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.9.5 Darwin/22.2.0

File hashes

Hashes for et_stopwatch-1.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1f44d63e030a9523a86acfdea6ea8544bdf2438c8b590ea01b9bc731c5137bcc
MD5 81d54c91d80918ac7f32ecb233bd7e1a
BLAKE2b-256 406c53fb50f615de967df9de5704309239e04138f8f623a6d6fc71e897f839a0

See more details on using hashes here.

Supported by

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