Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Sep 15, 2025

This PR enhances QuantEcon's timing functionality by updating the default precision and adding global precision control capabilities for modern timing functions.

Changes Made

1. Updated Default Precision

Timer class and timeit function now default to 4 decimal places instead of 2:

import quantecon as qe
import time

# Before: showed 0.01 seconds  
# After: shows 0.0124 seconds
with qe.Timer():
    time.sleep(0.01234)

2. Added Global Precision Control

Introduced qe.timings.float_precision() function to set precision globally for Timer and timeit:

import quantecon as qe

# Get current precision
current = qe.timings.float_precision()  # Returns 4

# Set precision for Timer and timeit functions globally
qe.timings.float_precision(6)

# Timer and timeit use 6 digits
with qe.Timer(): ...  # 6 decimal places
qe.timeit(func)       # 6 decimal places  

# Deprecated functions maintain original defaults
qe.tac()              # Still uses digits=2 default

# Reset to default
qe.timings.float_precision(4)

3. Affected Functions

The following functions now use the global precision by default:

  • Timer class context manager
  • timeit() function

The following deprecated functions maintain their original behavior:

  • tic(), tac(), toc() functions (continue using digits=2 default)
  • loop_timer() function (continues using digits=2 default)

4. Backward Compatibility

Explicit precision parameters still work and override the global setting:

# Still works - explicit precision overrides defaults
with qe.Timer(precision=8): ...
qe.tac(digits=6)

Implementation Details

  • Created new quantecon.timings module with precision management
  • Global precision only affects Timer and timeit functions (not deprecated functions)
  • Deprecated functions maintain original behavior for future deprecation
  • Added comprehensive test coverage (27 tests)
  • Maintains full backward compatibility
  • Input validation ensures precision is a non-negative integer

Testing

All 27 timing tests pass, covering:

  • Default precision validation (now 4 for Timer/timeit, still 2 for deprecated functions)
  • Global precision get/set functionality
  • Input validation
  • Integration with Timer and timeit functions
  • Explicit parameter override behavior
  • Original behavior preservation for deprecated functions

Fixes #804.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

- Create quantecon.timings module with float_precision() function
- Update Timer class to use global precision as default (was 2, now 4)
- Update timeit function to use global precision as default
- Update tac/toc/loop_timer functions to use global precision as default
- Add comprehensive tests for new global precision functionality
- All 27 timing tests passing (19 existing + 8 new)
- Backward compatibility maintained - explicit precision parameters still work

Co-authored-by: mmcky <[email protected]>
Copilot AI changed the title [WIP] ENH: update default precision for timer and enable setting precision globally ENH: Update default timing precision to 4 digits and add global precision control Sep 15, 2025
Copilot AI requested a review from mmcky September 15, 2025 07:20
Copilot finished work on behalf of mmcky September 15, 2025 07:20
@mmcky
Copy link
Contributor

mmcky commented Sep 15, 2025

@copilot I see you have updated

  • tic(), tac(), toc() functions
  • loop_timer() function

these functions will be deprecated in the future, so let's not change their behaviour. Can we update this PR to just adjust

  • Timer() context manager
  • timeit() function

@coveralls
Copy link

coveralls commented Sep 15, 2025

Coverage Status

coverage: 92.579% (+0.04%) from 92.539%
when pulling 58f397d on copilot/fix-804
into 08a6188 on main.

…and timeit using global precision

Co-authored-by: mmcky <[email protected]>
Copy link
Contributor Author

Copilot AI commented Sep 15, 2025

@copilot I see you have updated

  • tic(), tac(), toc() functions
    ...

Done! I've reverted the changes to the deprecated functions (tic/tac/toc/loop_timer) to maintain their original behavior with digits=2 default. Only the Timer context manager and timeit function now use the global precision control with 4-digit default.

The changes ensure the deprecated functions maintain backward compatibility exactly as before, while the modern Timer and timeit functions benefit from the enhanced precision control. All 27 timing tests pass.

Commit: 58f397d

Copilot AI changed the title ENH: Update default timing precision to 4 digits and add global precision control ENH: Update default timing precision to 4 digits and add global precision control for Timer and timeit Sep 15, 2025
Copilot finished work on behalf of mmcky September 15, 2025 07:31
@mmcky mmcky marked this pull request as ready for review September 15, 2025 07:41
@mmcky mmcky merged commit 6eaa314 into main Sep 15, 2025
22 checks passed
@mmcky mmcky deleted the copilot/fix-804 branch September 15, 2025 11:00
Copilot AI mentioned this pull request Sep 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ENH: update default precision for timer and enable setting precision globally

3 participants