INFR: Add Timer context manager for modern timing patterns#783
Conversation
|
@mmcky 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs. I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review. |
Co-authored-by: mmcky <8263752+mmcky@users.noreply.github.com>
|
This is an example of use within a lecture from
@jstac I quite like this style and the functionality that for comparison here is our existing
|
|
|
This is really nice @mmcky :-) I'm happy for you to go ahead. It will be really nice to get these changes propagated... |
|
thanks @jstac. I will double check the code and get this merged.
Note (@jstac): from a style perspective do you prefer this approach for timing code to using Jupyter cell magics As I see it the main downside is
|
|
I have usecases of |
Fully agree @oyamad. We are in no real rush to remove them, but I was thinking about issuing a DeprecationWarning with links to a migration guide -- perhaps in the next major release. #783 (comment) includes the pattern, but including in the notice is always helpful. Given we have an open issue (See #786) -- I will check that box off and we can treat that separately. @oyamad I have updated the QuantEcon manual with some more information https://manual.quantecon.org/styleguide/code.html#performance-timing-patterns |
) Emit a DeprecationWarning from the public `tic`, `tac`, `toc` and `loop_timer` functions, pointing users to the modern `Timer` context manager and `timeit` function added in QuantEcon#783/QuantEcon#794. Removal is targeted for v0.12.0. - `__Timer__.loop_timer` now calls its own methods (`self.tic`/`tac`/ `toc`) instead of the public wrappers, so a single `loop_timer` call emits exactly one warning rather than one per run. - The warning uses `stacklevel=3` so it is attributed to the user's call site rather than the wrapper. - Added `.. deprecated:: 0.12.0` directives to the module and the affected docstrings. - Added tests asserting each function warns, that `loop_timer` warns only once, and that the warning points at the caller. Note: the original issue mentioned removal in 0.9.*, but the library is already at v0.11.2, so this targets v0.12.0 instead.



This PR implements a
Timercontext manager to modernize timing patterns in QuantEcon.py, addressing the infrastructure need identified in the issue.Problem
The codebase currently uses manual timing patterns like:
This approach is verbose and error-prone, requiring manual time calculations and formatting.
Solution
Added a
Timercontext manager that provides clean, modern timing syntax:Features
timer.elapsedafter context exit for performance comparisonsImplementation Details
Timerclass to existingquantecon/util/timing.pymodulequantecon.Timerandquantecon.util.Timertic/tac/tocfunctionsUsage Examples
The Timer supports various usage patterns:
Fixes #782.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.