Commit 33c1590
timer + ETAEstimator: use perf_counter instead of monotonic
`unpythonic.misc.timer` and `unpythonic.timeutil.ETAEstimator`
both measured wall-clock time via `time.monotonic()`. Both are
monotonic (guaranteed since Python 3.3), but `monotonic`'s
resolution is implementation-defined, and on Windows it is backed
by a ~16 ms tick counter. `time.perf_counter()` is documented
as "the highest available resolution to measure a short
duration" and is also guaranteed monotonic.
The bug that forced the investigation was in `timer`: a PyPy-
JIT'd `for _ in range(int(1e6)): pass` block runs in microseconds,
which on Windows is **below** `monotonic`'s resolution, so
`timer().dt` registered as exactly `0.0`. Three testsets in
unpythonic's own suite then failed on windows-latest × pypy-3.11
in the expanded CI matrix:
- `test_misc::timer` — `test[tictoc.dt > 0]` held `0 > 0`, Fail.
- `test_fploop::performance benchmark` — `fp2.dt / ip.dt` was
ZeroDivisionError, Error.
- `test_tco::performance benchmark` — same ZeroDivisionError.
All three cascaded from the one root cause; switching the clock
in `timer` makes all three go green without touching the tests
themselves. Every other call site of `timer` in user code also
silently benefits.
`ETAEstimator` measures per-task timings on the order of seconds
to minutes, so `monotonic`'s resolution was not a correctness
problem at that scale. Switched for consistency and for the
documentation claim that `perf_counter` is the right tool for
measuring wall-clock elapsed within a single process. Both
classes only measure a dynamic extent in one process, so the
"comparable across processes" guarantee of `monotonic` that we
give up is not used anywhere.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>1 parent 97b9388 commit 33c1590
3 files changed
Lines changed: 16 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
| 18 | + | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| |||
124 | 124 | | |
125 | 125 | | |
126 | 126 | | |
127 | | - | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
128 | 137 | | |
129 | 138 | | |
130 | | - | |
| 139 | + | |
131 | 140 | | |
132 | 141 | | |
133 | 142 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
73 | | - | |
| 73 | + | |
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
| |||
79 | 79 | | |
80 | 80 | | |
81 | 81 | | |
82 | | - | |
| 82 | + | |
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
| |||
109 | 109 | | |
110 | 110 | | |
111 | 111 | | |
112 | | - | |
| 112 | + | |
113 | 113 | | |
114 | 114 | | |
115 | 115 | | |
| |||
0 commit comments