Skip to main content

CI Coverage Tests Mutation Docs PyPI DOI Python License code style: ruff OpenSSF Scorecard

counted_float logo

counted-float

This Python package provides functionality for...

  • counting floating point operations (FLOPs) of numerical algorithms implemented in plain Python, optionally weighted by their relative cost of execution
  • running benchmarks to estimate the relative cost of executing various floating-point operations (requires the benchmarking optional dependency)

Flop weights are computed using a highly curated dataset spanning a wide range of modern CPUs:

  • 21 benchmarks, 16 spec sheets, 12 third party measurements (Agner Fog, uops.info)
  • covering x86 (Intel, AMD) and ARM (Apple, AWS, Azure) architectures
Built-in flop weights, relative to ADD, per architecture

The target application area is evaluation of research prototypes of numerical algorithms where (weighted) flop counting can be useful for estimating total computational cost, in cases where benchmarking a compiled version (C, Rust, ...) is not feasible or desirable.

Full documentation: counted-float.readthedocs.io

Installation

Use your favorite package manager such as uv or pip. What you install decides which of the three capabilities you get:

pip install counted-float                  # counting
pip install counted-float[benchmarking]    # + measure this machine's flop costs
pip install counted-float[cli]             # + the counted_float command

Counting is the base install and needs nothing else. Building CountedFloat values, counting contexts, the built-in flop weights, reading benchmark results shipped with the package, and evaluating what counting costs you on your own workload all work here. It is about 17 MB installed.

Benchmarking measures your machine — running the flop benchmark suite to derive weights for the hardware you are on, rather than using the shipped consensus ones. It needs compiled probes (numba) and the packages that describe a CPU (psutil, py-cpuinfo), which is most of the install size: with it, expect roughly 180 MB. Without the extra, calling the benchmark suite tells you what to install instead of failing obscurely, and nothing else is affected.

The CLI adds the counted_float command. The command is always installed; without the extra it reports what to install instead of producing a traceback.

Extras compose, so counted-float[benchmarking,cli] gets you everything.

Quick start

CountedFloat is a drop-in replacement for the built-in float; it is "contagious", so results of math operations involving a CountedFloat stay CountedFloat:

from counted_float import CountedFloat

cf = CountedFloat(1.3)
f = 2.8

result = cf + f  # result = CountedFloat(4.1)

is_float_1 = isinstance(cf, float)  # True
is_float_2 = isinstance(result, float)  # True

FLOPs performed by CountedFloat values are counted while a FlopCountingContext is active:

from counted_float import CountedFloat, FlopCountingContext

cf1 = CountedFloat(1.73)
cf2 = CountedFloat(2.94)

with FlopCountingContext() as ctx:
    _ = cf1 * cf2
    _ = cf1 + cf2

counts = ctx.flop_counts()   # {FlopType.MUL: 1, FlopType.ADD: 1}
counts.total_count()         # 2

Performance overhead

CountedFloat adds counting overhead in two forms — the price of Python-level operator dispatch and result wrapping. How much slower your code runs depends almost entirely on its operation mix:

  • native float ops (+, -, *, /, comparisons): the expensive end of the range — the fixed per-operation dispatch cost dwarfs the nanoseconds of actual arithmetic;
  • patched math.* calls (math.sqrt, math.lgamma, …): a roughly fixed surcharge per call, so the multiple shrinks as the function itself gets more expensive.

counted_float evaluate-overhead measures your own machine — a per-flop-type overhead table, the geomean across types, and a practical mixed workload; see the captured example for representative figures.

Three facts worth knowing:

  • counting state is per-thread: a FlopCountingContext measures only the thread that opened it (open one context per worker thread to measure multi-threaded code, and sum the results). Free-threaded builds (3.14t) are supported and CI-tested;
  • the overhead is inherent and PauseFlopCounting does not reduce it (the instrumented operators still execute; only count registration stops) — the escape hatch for hot uncounted regions is converting back via float(x);
  • overhead never affects count accuracy — counts are exact regardless.

This makes CountedFloat a tool for research and prototyping code, not production hot loops.

numpy counting is an explicit non-goal: np.float64 (float subclass) scalars work and count correctly, but mixing CountedFloat with numpy arrays raises TypeError rather than silently returning uncounted results — see Known limitations for the full boundary.

Documentation

The documentation site covers the rest:

Metadata

Release files for counted-float 2.7.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 counted-float 2.7.0
File Size Uploaded
counted_float-2.7.0.tar.gz 188.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for counted-float 2.7.0
File Interpreter ABI Platform
counted_float-2.7.0-py3-none-any.whl Python 3 none any Details

Total release size: 434.9 kB

Release files / counted_float-2.7.0.tar.gz

Download URL counted_float-2.7.0.tar.gz
Size 188.0 kB
Tags Source
SHA-256 checksum
How to use checksums
4b7d40147ee6cefb6f442ca3ca138e17239c85cca78fe6d4526588a6ecd237b1
BLAKE2b-256 checksum
How to use checksums
a5236116d3a4dd57066d8132e9692f70136ce98d577e68323af28b589d378286
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 10, 2026.

Transparency log

Release files / counted_float-2.7.0-py3-none-any.whl

Download URL counted_float-2.7.0-py3-none-any.whl
Size 246.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4f84d9c436145bd239b48d31fbfab0ce4297101effc483376efed9835aa7d264
BLAKE2b-256 checksum
How to use checksums
680f95173fd665bbf4eb6d09e0ba3f5017c37b47dab6690e9bf15d3a93915f5b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 10, 2026.

Transparency log

Release history Release notifications | RSS feed

2.8.0

2 release files

This release

2.7.0 This release

2 release files

2.6.0

2 release files

2.5.0

2 release files

2.4.0

2 release files

2.3.1

2 release files

2.3.0

2 release files

2.2.2

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.5

2 release files

2.0.4

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.7.0

2 release files

1.6.3

2 release files

1.6.2

2 release files

1.6.1

2 release files

1.6.0

2 release files

1.5.2

2 release files

1.5.1

2 release files

1.5.0

2 release files

1.4.2

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.4

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.0

2 release files

0.9.7

2 release files

0.9.6

2 release files

0.9.5

2 release files

0.9.4

2 release files

0.9.3

2 release files

0.9.2

2 release files

0.9.1

2 release files

0.8.4

2 release files

0.8.3

2 release files

0.8.2

2 release files

0.8.1

2 release files

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