Skip to main content

A Stopwatch for timing blocks of code

Project description

Python Stopwatch

This module contains a class and a function to time your code easily

Below are some examples:

First import the Stopwatch class

from stopwatch import Stopwatch

For the purpose of the tutorial were going to use this function which takes about 1.5 seconds to run

def func():
    x = ''
    for i in range(1_000_000):
        x += str(i)

Using the with statement:

with Stopwatch() as sw:
    func()
    print(sw.time_elapsed)
1.4438485999708064

Or you can print the instance itself, which will return a nicely formatted string:

print(sw)
Time elapsed: 1.444

You can also a pass an instance of a class to automatically add the time taken to the total elapsed time of the insatnce:

sw = Stopwatch()

with sw:
    func()

func()

with sw:
    func()
    
print(sw)
'2.7854167000041343'

The above example is the equivalent of:

sw = Stopwatch()

sw.start()
func()
sw.stop()

func()

sw.start()
func()
sw.stop()

print(sw)
'2.9421735999640077'

You can also use the decorator to time a function every time you run it

First import the decorator:

from stopwatch import time_it

Add it to your function:

@time_it()
def func():
    x = ''
    for i in range(1_000_000):
        x += str(i)

Now it will return a tuple with the time taken and the output of the function (which in our case is None)

print(func())
(1.1367347000050358, None)

You can also add the time taken to an existing instance of Stopwatch like so:

sw = Stopwatch()

@time_it(stopwatch=sw)
def func():
    x = ''
    for i in range(1_000_000):
        x += str(i)

Call the function twice:

print('Out:', func())
print('Total time elapsed:', sw.time_elapsed)
print('Out:', func())
print('Total time elapsed:', sw.time_elapsed)
Out: (1.3642582000466064, None)
Total time elapsed: 1.3642542000161484
Out: (1.3336866000317968, None)
Total time elapsed: 2.6979382000281475

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

ez-stopwatch-1.0.tar.gz (4.4 kB view hashes)

Uploaded Source

Built Distribution

ez_stopwatch-1.0-py3-none-any.whl (5.1 kB view hashes)

Uploaded Python 3

Supported by

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