Skip to main content
Continuous Integration Test Coverage Documentation Latest Version Python versions MIT License

Yet another simple time measurement tool for Python. The goal of this implementation is to avoid as much cruft as possible. The current version is 72 lines of actual code long, leaving out blank, doc and comment lines. Chronometer provides only functions to measure how much wall-clock time has passed between starting and stopping the timer.

Nothing more. Nothing less.

Chronometer tries to stay accurate to the actual time spent between starting and stopping the timer by utilizing a monotonic timer. According to the linux manual a monotonic timer is subject to time adjustments so it stays accurate but will never move backwards or jump. It will be adjusted gradually and always moves forward as long as the system runs.

Examples

Easy:

import time
from chronometer import Chronometer

long_running_task = lambda: time.sleep(3.)

with Chronometer() as t:
    long_running_task()  # that will take a few seconds.
print('Phew, that took me {:.3f} seconds!'.format(float(t)))

Advanced:

from time import sleep
from chronometer import Chronometer

counter = 0
def long_running_task_that_can_fail():
    global counter
    counter += 1
    sleep(2.)
    return counter > 3

with Chronometer() as t:
    while not long_running_task_that_can_fail():
        print('Failed after {:.3f} seconds!'.format(t.reset()))
print('Success after {:.3f} seconds!'.format(float(t)))

Ridiculous:

import asyncio
from chronometer import Chronometer


class PingEchoServerProtocol(asyncio.StreamReaderProtocol):

    def __init__(self):
        super().__init__(asyncio.StreamReader(), self.client_connected)
        self.reader, self.writer = None, None
        self.latency_timer = Chronometer()

    def client_connected(self, reader, writer):
        self.reader, self.writer = reader, writer
        asyncio.async(self.ping_loop())
        asyncio.async(self.handler())

    @asyncio.coroutine
    def send(self, data):
        self.writer.write(data.encode('utf-8') + b'\n')
        yield from self.writer.drain()

    @asyncio.coroutine
    def ping_loop(self):
        yield from asyncio.sleep(5.)
        while True:
            if self.latency_timer.stopped:
                self.latency_timer.start()
                yield from self.send('PING (send me PONG!)')

            sleep_duration = max(2., 10. - self.latency_timer.elapsed)
            yield from asyncio.sleep(sleep_duration)

    @asyncio.coroutine
    def handler(self):
        while True:
            data = (yield from self.reader.readline())
            if data[:4] == b'PONG' and self.latency_timer.started:
                yield from self.send(('Latency: {:.3f}s'
                                      .format(self.latency_timer.stop())))

l = asyncio.get_event_loop()

@asyncio.coroutine
def startup():
    s = (yield from l.create_server(lambda: PingEchoServerProtocol(),
                                    host='localhost', port=2727))
    print('Now telnet to localhost 2727')
    yield from s.wait_closed()

l.run_until_complete(startup())

Metadata

Release files for chronometer 1.0

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

Source distribution (sdist)

Source distribution for chronometer 1.0
File Size Uploaded Provenance
chronometer-1.0.zip 12.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for chronometer 1.0
File Interpreter ABI Platform
chronometer-1.0-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 18.6 kB

Release files / chronometer-1.0.zip

Download URL chronometer-1.0.zip
Size 12.4 kB
Tags Source
SHA-256 checksum
How to use checksums
ff31478af4ea0e7cb8590702b2605d300e6f4462c52fd3157cd7db18bf2b060f
BLAKE2b-256 checksum
How to use checksums
d5a693bb2c89176c5517033b3409f506e5404f4495d2ba73d9fa45962d844308
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / chronometer-1.0-py2.py3-none-any.whl

Download URL chronometer-1.0-py2.py3-none-any.whl
Size 6.3 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
9722198bf8dfd4880da429b739738c0d09f04a5ad901ed43273a092f7978a483
BLAKE2b-256 checksum
How to use checksums
8b2e487bb336e1548506c86d06f818c9ab3014122272773688392ddf08062bbe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for chronometer 1.0

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