Approx 1.5x speedup in weekday in Datetime using pre computed odd days - #157933
Closed
utkarsh-naman wants to merge 1 commit into
Closed
utkarsh-naman wants to merge 1 commit into
utkarsh-naman wants to merge 1 commit into
Conversation
utkarsh-naman
requested review from
StanFromIreland and
pganssle
as code owners
September 21, 2026 22:33
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
Member
|
Please create an issue first to discuss this and show that we have real improvements in usercode. I do not think this one js necessary |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
gh-NNNNNN: Optimize date.weekday() using precomputed odd days
Summary
Summary
This PR optimizes the weekday calculation in
datetimeby using precomputed odd-day values for months and a reduced calculation for the number of odd days before a given year.The optimization is implemented in both the pure-Python implementation and the C implementation:
Lib/_pydatetime.pyModules/_datetimemodule.cThe existing weekday behavior and Gregorian calendar rules are preserved.
Implementation
The optimization introduces a lookup table containing the number of odd days before each month:
The number of odd days before the start of a year is then computed using the Gregorian 400-year cycle rather than calculating the full number of days before the year.
For dates after February in a leap year, the additional leap day is added to the ordinal calculation.
The resulting weekday calculation is:
followed by modulo 7.
The corresponding logic has been added to both
_pydatetime.pyand_datetimemodule.c.The C implementation similarly adds the month lookup table, the optimized year calculation, and uses the resulting ordinal in
weekday().Correctness
The optimized calculation was exhaustively tested against an independent Gregorian weekday implementation using every valid date in the supported
datetimerange.Results:
The reference implementation used for the verification was Sakamoto's Gregorian weekday algorithm, which is independent of the odd-day implementation.
The exhaustive test therefore covers:
Performance
The purpose of this change is to reduce the arithmetic required for weekday calculation by replacing repeated day-count calculations with precomputed month values and reduced year arithmetic.
The performance claim should be based on a
pyperfbenchmark of the actual CPython implementation.Testing
An independent exhaustive C test was used to verify the weekday calculation.
The complete supported Gregorian range was tested:
All 3,652,059 valid dates matched the independent reference implementation.
Files changed
Lib/_pydatetime.pyModules/_datetimemodule.cBackward compatibility
This change does not modify the public
datetimeAPI or Gregorian calendar behavior.date.weekday()continues to return:Only the internal calculation used to obtain the weekday has been changed.
Motivation
weekday()is a small, frequently used operation, so reducing the arithmetic required for the calendar calculation can improve its execution time while retaining the existing behavior.The optimization also takes advantage of the fact that the Gregorian calendar repeats every 400 years:
Therefore, complete 400-year cycles do not affect the resulting weekday.