Suor / funcy

A fancy and practical functional tools
BSD 3-Clause "New" or "Revised" License
3.38k stars 144 forks source link

use more precise timer `timeit.default_timer` for log_durations #118

Closed skshetry closed 2 years ago

skshetry commented 2 years ago

Instead of using time.time, this uses `timeit.default_timer()`. default_timer uses time.perf_counter which is monotonic, is not user-adjustable, and is more precise.

However, time.perf_counter is only available since Python>3.3, and hence is unavailable in Python 2.7. In that version, it defaults to time.clock() in Windows and time.time() in other platforms.

I think a similar change should be made on cache and throttle functions.

From discussion in https://github.com/iterative/dvc/pull/7393#discussion_r854320578

Suor commented 2 years ago

Since funcy still supports 2.7, this should go to compat.py:

try:
    from timeit import default_timer
except ImportError:
    from time import perf_counter as default_timer

And then use it everywhere.

skshetry commented 2 years ago

@Suor, this uses timeit.default_timer which is available in 2.7, so need for compat.

skshetry commented 2 years ago

Also, there is no monotonic timer in Python 2.7, it was introduced in Python 3.3.

Suor commented 2 years ago

Ok, thanks.

skshetry commented 2 years ago

@Suor, WDYT of cache and throttle?

Suor commented 2 years ago

They are fine using time.time(). Additionally throttle() exposes the .blocked_until value, so changing it to something else might break something.