Skip to main content

CI Coverage Tests Docs PyPI 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 numba optional dependency for achieving accurate results)

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.

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

Full documentation: counted-float.readthedocs.io

Installation

Use your favorite package manager such as uv or pip:

pip install counted-float           # install without optional dependencies
pip install counted-float[numba]    # install with numba optional dependency
pip install counted-float[cli]      # install with CLI support (click)

Numba is optional due to its relatively large size (40-50MB, including llvmlite), but without it, benchmarks will not be reliable (but will still run, but not in jit-compiled form).

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. Measured on an Apple M3 Max (measure your own machine with counted_float benchmark-counted-float):

  • native float ops (+, -, *, /, comparisons): roughly 20–40× slower than plain float per operation, environment-dependent (~21× on the M3 Max bisection benchmark);
  • patched math.* calls (math.sqrt, math.exp, …): a roughly fixed ~0.1 µs of overhead per call — about 6–7× for cheap functions like sqrt, and a smaller multiple for costlier ones (the fixed overhead is a smaller share of a slower call).

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:

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

counted_float-2.0.0.tar.gz (161.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

counted_float-2.0.0-py3-none-any.whl (217.9 kB view details)

Uploaded Python 3

File details

Details for the file counted_float-2.0.0.tar.gz.

File metadata

  • Download URL: counted_float-2.0.0.tar.gz
  • Upload date:
  • Size: 161.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for counted_float-2.0.0.tar.gz
Algorithm Hash digest
SHA256 b89e291e80d7e1c686929adde408ec355afb437780f141251a64a80bef7ca4cf
MD5 79af88c9c628f59f39a804adf3b1acf1
BLAKE2b-256 7c72b9ba4cae7e8aacbba1263ba8f14df7079123122aa45e4bfa966bbb397da5

See more details on using hashes here.

Provenance

The following attestation bundles were made for counted_float-2.0.0.tar.gz:

Publisher: release_tag.yml on bertpl/counted-float

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file counted_float-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: counted_float-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 217.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for counted_float-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2a8e7126f23acd1a563c8f740d40c3c6422c8f909108c2137f7758f010eccab9
MD5 790731995136167640bf630cbf8bd4a1
BLAKE2b-256 5a04681b6764eb01d5c8e5287721b768435eacca7117d955f47c1087957d2641

See more details on using hashes here.

Provenance

The following attestation bundles were made for counted_float-2.0.0-py3-none-any.whl:

Publisher: release_tag.yml on bertpl/counted-float

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

2.6.0

2 files

2.5.0

2 files

2.4.0

2 files

2.3.1

2 files

2.3.0

2 files

2.2.2

2 files

2.2.1

2 files

2.2.0

2 files

2.1.1

2 files

2.1.0

2 files

2.0.5

2 files

2.0.4

2 files

2.0.3

2 files

2.0.2

2 files

2.0.1

2 files

This release

2.0.0 This release

2 files

1.7.0

2 files

1.6.3

2 files

1.6.2

2 files

1.6.1

2 files

1.6.0

2 files

1.5.2

2 files

1.5.1

2 files

1.5.0

2 files

1.4.2

2 files

1.4.1

2 files

1.4.0

2 files

1.3.0

2 files

1.2.2

2 files

1.2.1

2 files

1.2.0

2 files

1.1.4

2 files

1.1.3

2 files

1.1.2

2 files

1.1.1

2 files

1.1.0

2 files

1.0.5

2 files

1.0.4

2 files

1.0.3

2 files

1.0.2

2 files

1.0.0

2 files

0.9.7

2 files

0.9.6

2 files

0.9.5

2 files

0.9.4

2 files

0.9.3

2 files

0.9.2

2 files

0.9.1

2 files

0.8.4

2 files

0.8.3

2 files

0.8.2

2 files

0.8.1

2 files

0.8.0

2 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