Skip to content

Compare timings in nanoseconds for sub-microsecond precision - #23

Merged
jaraco merged 3 commits into
mainfrom
feature/sub-microsecond-precision
Jul 14, 2026
Merged

Compare timings in nanoseconds for sub-microsecond precision#23
jaraco merged 3 commits into
mainfrom
feature/sub-microsecond-precision

Conversation

@jaraco

@jaraco jaraco commented Jul 13, 2026

Copy link
Copy Markdown
Owner

Closes #18

Problem

Result parsed timeit's output into a datetime.timedelta, whose resolution bottoms out at one microsecond. For sub-microsecond exercises — e.g. the name-normalization work in python/importlib_metadata#533 — both the control and experiment rounded to timedelta(0), collapsing variance = (experiment - control) / control to 0/0 and rendering any regression or improvement invisible.

Fix

Parse with the new tempora.parse_nanoseconds (jaraco/tempora#49, released in tempora 5.11), keeping durations as a Decimal count of nanoseconds so the delta and variance stay meaningful at any scale. Since the values are no longer timedeltas, Result.__str__ now formats the experiment and delta with timeit's own units (nsec/usec/msec/sec) instead of timedelta's H:MM:SS rendering.

Result

The suite's own perf exercises now report meaningful sub-microsecond numbers where they previously would have been timedelta(0):

check_perf_isolated:   553 nsec  (+7 nsec, 1%)
with deps and extras:  10.5 nsec (+0 nsec, 0%)
diff_from_oh_nine_two: 2.95 nsec (-0.02 nsec, -1%)
simple test:           525 nsec  (-35 nsec, -6%)

Bumps the tempora floor to >=5.11 for parse_nanoseconds. Full suite green locally against tempora 5.11.0.

🤖 Generated with Claude Code

Result parsed timeit's output into a datetime.timedelta, whose
resolution bottoms out at one microsecond. For sub-microsecond
exercises -- e.g. the name-normalization work in python/importlib_metadata#533
-- both the control and experiment rounded to timedelta(0), collapsing
the variance calculation to 0/0 and rendering any regression or
improvement invisible.

Parse instead with tempora.parse_nanoseconds, keeping durations as a
Decimal count of nanoseconds so the delta and variance stay meaningful
at any scale. Because the values are no longer timedeltas, format the
reported experiment and delta with timeit's own units (nsec/usec/msec/sec)
rather than timedelta's H:MM:SS rendering.

Closes #18.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Comment thread pytest_perf/runner.py Outdated
Comment thread pytest_perf/runner.py Outdated
Comment thread pytest_perf/runner.py Outdated
Comment thread pytest_perf/runner.py Outdated
Comment thread pytest_perf/runner.py Outdated
Comment thread pytest_perf/runner.py Outdated
….signed

Rework the previous commit's approach in response to review. Parsing to a
bare Decimal count of nanoseconds retained the needed precision but
reduced a duration to a scalar with implied units, and left the unit
formatting -- which isn't pytest-perf's concern -- living here.

Parse instead into a tempora.Duration (5.12): a nanosecond-resolution
timedelta that carries its own magnitude and units, divides by another
Duration to yield the variance ratio directly, and renders itself in
timeit's natural units (nsec/µsec/msec/sec). The reported delta gets its
sign from jaraco.functools.signed (4.6), replacing a boolean flag on the
formatter. variance returns to a try/except, and all duration formatting
leaves this module.

Requires tempora 5.12 (Duration) and jaraco.functools 4.6 (signed).
Closes #18.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Comment thread test_runner.py Outdated
Add a doctest exercising Result: parsing, delta, variance (including the
zero-control branches that return inf or 0), and rendering. Inline the
signed delta formatter into __str__ rather than binding it at module
scope.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
@jaraco
jaraco force-pushed the feature/sub-microsecond-precision branch from 18bb5cf to 8b9151f Compare July 14, 2026 09:04
@jaraco
jaraco merged commit 1df0c50 into main Jul 14, 2026
30 of 32 checks passed
@jaraco
jaraco deleted the feature/sub-microsecond-precision branch July 14, 2026 09:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Better support for sub-microsecond operations

1 participant