Python: how time-machine is O(1) where freezegun is O(n) 0 ▲ Adam Johnson 34 minutes ago · 9 min read1775 words · Tech · hide · 0 comments time-machine is my library for mocking the current date and time in Python tests. Its headline advantage over freezegun, the library that inspired it, is speed. Back in 2021, I benchmarked the two libraries at two project sizes, and found time-machine 100 to 200 times faster. That post asserted that freezegun’s work grows with the number of imported modules, whilst time-machine’s does not. Five years on, following many time-machine optimizations, including in Friday’s time-machine 3.3.0, let’s re-benchmark the two libraries to empirically measure their runtime complexity and examine their code to explain why. The benchmark I wrote a benchmark script to compare the two libraries, listed in full at the end of this section. The variable here is the number of attributes across all imported modules. To roughly simulate production code, the benchmark generates modules as bare types.ModuleType instances and stuffs them straight into sys.modules. Each module object has 25 attributes, and one… No comments yet. Log in to reply on the Fediverse. Comments will appear here.