Title: Remove `TODO` comment in `_pydatetime._isoweek1monday` · Issue #127553 · python/cpython · GitHub
Open Graph Title: Remove `TODO` comment in `_pydatetime._isoweek1monday` · Issue #127553 · python/cpython
X Title: Remove `TODO` comment in `_pydatetime._isoweek1monday` · Issue #127553 · python/cpython
Description: Feature or enhancement Proposal: According to the existing note # XXX This could be done more efficiently, the _isoweek1monday function can be improved. Current Implementation: def _isoweek1monday(year): # Helper to calculate the day num...
Open Graph Description: Feature or enhancement Proposal: According to the existing note # XXX This could be done more efficiently, the _isoweek1monday function can be improved. Current Implementation: def _isoweek1monday(...
X Description: Feature or enhancement Proposal: According to the existing note # XXX This could be done more efficiently, the _isoweek1monday function can be improved. Current Implementation: def _isoweek1monday(...
Opengraph URL: https://github.com/python/cpython/issues/127553
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Remove `TODO` comment in `_pydatetime._isoweek1monday`","articleBody":"# Feature or enhancement\n\n### Proposal:\n\nAccording to the existing note `# XXX This could be done more efficiently`, the `_isoweek1monday` function can be improved.\n\n### Current Implementation:\n\n```python\ndef _isoweek1monday(year):\n # Helper to calculate the day number of the Monday starting week 1\n # XXX This could be done more efficiently\n THURSDAY = 3\n firstday = _ymd2ord(year, 1, 1)\n firstweekday = (firstday + 6) % 7 # See weekday() above\n week1monday = firstday - firstweekday\n if firstweekday \u003e THURSDAY:\n week1monday += 7\n return week1monday\n```\n\n### Proposed Change\nReplace the current implementation with a simplified logic that is both more performant and easier to understand.\n```python\ndef _isoweek1monday_new(year):\n # Calculate the ordinal day of the Monday starting ISO week 1.\n # ISO week 1 is defined as the week that contains January 4th.\n jan4 = _ymd2ord(year, 1, 4)\n jan4_weekday = (jan4 + 6) % 7\n return jan4 - jan4_weekday\n```\n\n### Rationale\n\nThe new logic:\n\t1.\tAligns directly with the ISO week definition, which makes it easier to comprehend.\n\t2.\tReduces unnecessary computations, improving performance slightly.\n\n\n### Validation and Performance Comparison\nUsing timeit to compare the current and proposed implementations:\n```python\nimport timeit\nfrom _pydatetime import _isoweek1monday, _ymd2ord\n\ndef _isoweek1monday_new(year):\n jan4 = _ymd2ord(year, 1, 4)\n jan4_weekday = (jan4 + 6) % 7\n return jan4 - jan4_weekday\n\n# Validation results:\n# \u003e\u003e\u003e all(_isoweek1monday_new(i) == _isoweek1monday(i) for i in range(2000))\n# True\n\n# Timing results:\n# \u003e\u003e\u003e timeit.timeit('_isoweek1monday(2000)', globals=globals())\n# 0.4805744589539245\n# \u003e\u003e\u003e timeit.timeit('_isoweek1monday_new(2000)', globals=globals())\n# 0.4299807079951279\n```\n\n### Has this already been discussed elsewhere?\n\nNo response given\n\n### Links to previous discussion of this feature:\n\n_No response_\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-127564\n* gh-128500\n* gh-128501\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/bombs-kim","@type":"Person","name":"bombs-kim"},"datePublished":"2024-12-03T11:30:26.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":11},"url":"https://github.com/127553/cpython/issues/127553"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:91872de8-3a7b-3259-b3c3-399645a4ca4e |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8F90:9124A:F062C6:1387163:696B0600 |
| html-safe-nonce | 39cead0921559346a8aed13bec2809b38bda84b47ec5b9aead91d478f7c21429 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4RjkwOjkxMjRBOkYwNjJDNjoxMzg3MTYzOjY5NkIwNjAwIiwidmlzaXRvcl9pZCI6IjIyNDY4OTYyMjEyMjcyNTUyOTYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 9a09e558d3280ddcbc99a3ce344ca9b3e1d14e1ea94c50f1dafc95db684595f1 |
| hovercard-subject-tag | issue:2714757112 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/127553/issue_layout |
| twitter:image | https://opengraph.githubassets.com/76e3d76d7b8497fb115ecdd26b81eef56b86d932bcc5f514f6fc0c48b212c564/python/cpython/issues/127553 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/76e3d76d7b8497fb115ecdd26b81eef56b86d932bcc5f514f6fc0c48b212c564/python/cpython/issues/127553 |
| og:image:alt | Feature or enhancement Proposal: According to the existing note # XXX This could be done more efficiently, the _isoweek1monday function can be improved. Current Implementation: def _isoweek1monday(... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | bombs-kim |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5f99f7c1d70f01da5b93e5ca90303359738944d8ab470e396496262c66e60b8d |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 82560a55c6b2054555076f46e683151ee28a19bc |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width