Skip to main content
# hwcounter

Highly accurate counter for measuring elapsed time in Python.

## Installation

``` shell
$ pip install hwcounter
```

## Overview

This Python extension module uses the hardware timestamp counter to
provide very high precision and accurate measurements of execution
time.

The module exposes three main objects. Two are plain functions,
`count` and `count_end`, which return the current value of the
timestamp counter. They can be used together to manually track the
start and end of a timing measurement. The third is the `Timer` type,
which is to be used as a context manager, wrapping a block of code to
be measured.

Using `Timer` to wrap a block of code is to be preferred over
manually using `count` and `count_end`. This is for convenience, as well
as because it factors in the overhead of calling the underlying
measurement instructions.

### `count()`

Returns the current value of the timestamp counter, in cycles.

### `count_end()`

Returns the current value of the timestamp counter, in cycles. It is
suitable for use at the end of a timing measurement.

### `Timer`

Class that implements the context manager protocol.

#### `Timer.cycles`

The `cycles` attribute is populated with the elapsed time, in cycles,
when the Timer context manager exits.

## Example usage

``` python
from hwcounter import Timer, count, count_end

from time import sleep
from math import sqrt


# 1. Manually count cycles elapsed between two points

start = count()
sqrt(144) / 12
elapsed = count_end() - start
print(f'elapsed cycles: {elapsed}')
# elapsed cycles: 36486


# 2. Use Timer object as context manager to wrap a block of code and measure its timing

with Timer() as t:
sleep(1)
print(f'elapsed cycles: {t.cycles}')
# elapsed cycles: 2912338344
```

These examples were performed on an Intel Core i5-6267U CPU @
2.90GHz. Notice that the sleep for 1 second in the example above
yields a 2.9 billion cycle count.

## Notes

The overhead of calling the underlying measurement instructions is
taken into account when using the Timer context manager. In other
words, the number of cycles it takes to call the machine instructions
are subtracted from the elapsed cycle count automatically.

This library returns measurements in processor clock cycles. For
benchmarking programs and making apples-to-apples comparisons of
changes in code execution time, this method is sufficient and
reliable. If elapsed time in seconds is desired, a conversion from
clock cycles is required: divide the cycle count by the processor's
clock speed (in Hz). This conversion is outside the scope of this
module.

## Portability

This extension uses the `RDTSC` and `RDTSCP` instructions on the x86
architecture, so it won't work on other platforms. It is Python 3
only.


Release files for hwcounter 0.1.1

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

Source distribution (sdist)

Source distribution for hwcounter 0.1.1
File Size Uploaded
hwcounter-0.1.1.tar.gz 4.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for hwcounter 0.1.1
File Interpreter ABI Platform
hwcounter-0.1.1-cp36-cp36m-macosx_10_6_intel.whl CPython 3.6 CPython 3.6 pymalloc macOS 10.6+ Intel (x86-64, i386) Details

Total release size: 12.3 kB

Release files / hwcounter-0.1.1.tar.gz

Download URL hwcounter-0.1.1.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
cfae7bea05b0d4008e4d32b1734afb9ef05c909e7c80274b0c9e0f8754ddde27
BLAKE2b-256 checksum
How to use checksums
838d855522fe8beb584e7ce748e551122943607655628c7a4a1be0270e2d8c07
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / hwcounter-0.1.1-cp36-cp36m-macosx_10_6_intel.whl

Download URL hwcounter-0.1.1-cp36-cp36m-macosx_10_6_intel.whl
Size 8.2 kB
Tags CPython 3.6 CPython 3.6 pymalloc macOS 10.6+ Intel (x86-64, i386)
SHA-256 checksum
How to use checksums
c7a4507f2e90be662fca17632b9411e7138cbd300ddb15907baac1d5f924c632
BLAKE2b-256 checksum
How to use checksums
06033b9c197b3bb343cf3de30f6595bcdc4431b78b2f0005640ec953cdecb5e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.1 This release

2 release files

0.1.0

2 release files

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