Adam Johnson
Read post

Python: how time-machine is O(1) where freezegun is O(n)

A deep-dive comparison of two Python time-mocking libraries — time-machine and freezegun — explaining why time-machine is O(1) while freezegun is O(n). Benchmarks show freezegun's mock/unmock cycle grows linearly with the number of loaded modules (about 2.5 µs per module), while time-machine stays constant at ~1.5 µs regardless of project size. At 16,000 modules, time-machine is 27,300x faster. The difference comes down to approach: freezegun scans all sys.modules to rebind references, while time-machine directly overwrites C function pointers, affecting every reference at the C layer. The post also covers freezegun's leakiness (missed class attributes, closures, changed types) and introduces a migration CLI to automate switching from freezegun to time-machine.

    #python#testing
Aug 03•10m read time•From adamj.eu
Post cover image
Table of contents
The benchmarkWhy freezegun is O(n): the sweepWhy time-machine is O(1): the swapMigrate today with time-machine’s migration CLIFin
127 Impressions
Adam Johnson's image
Adam Johnson

AdamJ's platform covers topics related to software development, technology trends, and coding tutor...

54 Followers

•

444 Upvotes

Would you recommend this post?

Copy link
WhatsApp
Facebook
X
New Squad
  • © 2026 Daily Dev Ltd.
  • Guidelines
  • Explore
  • Tags
  • Sources
  • Squads
  • Leaderboard