Skip to content

bpo-41303: Use mac_continuous_time for perf_counter on macOS 10.12+ #21719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Doc/library/time.rst
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ Functions
point of the returned value is undefined, so that only the difference between
the results of consecutive calls is valid.

.. note:: On macOS versions prior to 10.12, perf_counter does not track time elapsed during sleep.
.. versionadded:: 3.3

.. function:: perf_counter_ns() -> int
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,7 @@ Dave Kuhlman
Jon Kuhn
Ilya Kulakov
Upendra Kumar
Rishav Kundu
Toshio Kuratomi
Ilia Kurenkov
Vladimir Kushnir
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
time.perf_counter now tracks time elapsed during sleep on macOS 10.12+.
13 changes: 13 additions & 0 deletions Python/pytime.c
Original file line number Diff line number Diff line change
Expand Up @@ -835,17 +835,30 @@ pymonotonic(_PyTime_t *tp, _Py_clock_info_t *info, int raise)
return -1;
}

#ifdef __MAC_10_12
t0 = mach_continuous_time();
#else
t0 = mach_absolute_time();
#endif
}

if (info) {
#ifdef __MAC_10_12
info->implementation = "mach_continuous_time()";
#else
info->implementation = "mach_absolute_time()";
#endif
info->resolution = (double)timebase.numer / (double)timebase.denom * 1e-9;
info->monotonic = 1;
info->adjustable = 0;
}

#ifdef __MAC_10_12
ticks = mach_continuous_time();
#else
ticks = mach_absolute_time();
#endif

/* Use a "time zero" to reduce precision loss when converting time
to floatting point number, as in time.monotonic(). */
ticks -= t0;
Expand Down