freezegun icon indicating copy to clipboard operation
freezegun copied to clipboard

Freezegun doesn't freeze `timeit.default_timer()`

Open wolph opened this issue 7 years ago • 3 comments

A little ipython test:

In [1]: import freezegun

In [2]: import timeit

In [3]: import time

In [4]: with freezegun.freeze_time():
   ...:     print(timeit.default_timer())
   ...:     time.sleep(1)
   ...:     print(timeit.default_timer())
   ...:
2332566.128189347
2332567.128278099

In [5]: with freezegun.freeze_time() as fake_time:
   ...:     print(timeit.default_timer())
   ...:     fake_time.tick(1)
   ...:     print(timeit.default_timer())
   ...:
2332733.968442932
2332733.968543587

wolph avatar Nov 15 '18 00:11 wolph

In Python 2.7, it's ok. Because Python 2.7 timeit.default_timer is just time.time.

timeit.py#L75

if sys.platform == "win32":
    # On Windows, the best timer is time.clock()
    default_timer = time.clock
else:
    # On most other platforms the best timer is time.time()
    default_timer = time.time

Bust Python3 change it to time.perf_counter, which is not coverd in freezegun

timeit.py#L61


default_timer = time.perf_counter

Hanaasagi avatar Jan 12 '19 14:01 Hanaasagi

Sounds like we should change this issue to be a feature request for time.perf_counter.

spulec avatar Jul 09 '19 01:07 spulec

Any update on this? Thanks in advance for your time!

NathanUrwin avatar Jul 14 '22 00:07 NathanUrwin