time-machine versus freezegun, a benchmark2021-02-19
I wrote my library time-machine last year as a way to speed up tests that need to accurately mock the current time.
The incumbent time mocking library, freezegun, mocks time by replacing every import of the date and time functions. To do this, it has to scan all attributes of all imported modules. Its runtime is thus proportional to the number of module level attributes in your project and dependencies.
time-machine takes a different approach. It mocks the date and time functions at the C layer, changing their pointers once. Its runtime is therefore constant, no matter how large your project grows.
This post covers a quick benchmark to demonstrate this difference. Even in a minimal setup, time-machine is 100 times faster than freezegun. Using a small Django project, the gap grows to time-machine being 200 times faster.
I set up the benchmark in a fresh
ipython session by creating test functions for each library.
These mock and unmock time with the same target datetime:
I then used the
%timeit ipython “magic” command to invoke Python’s
timeit module on each function:
freezegun took 6.4 milliseconds per call, whilst time-machine took 16 microseconds. So time-machine came out over 100 times faster.
ipython session had 647 imported modules:
I repeated the benchmark in an
ipython session on my new Django project, which has about three times the number of imported modules:
The new results:
freezegun’s runtime nearly doubled, whilst time-machine stayed constant.
This Django project is in its infancy. Longer running projects can have 10 or even 100 times as many modules, which can make using freezegun so slow it can dominate test run time.
May your test runtime grow linearly,
Want better tests? Check out my book Speed Up Your Django Tests which teaches you to write faster, more accurate tests.
One summary email a week, no spam, I pinky promise.
- How to Mock the Current Date and Time in Python
- Introducing time-machine, a New Python Library for Mocking the Current Time
© 2021 All rights reserved.