You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
date.today() is half as fast as datetime.now().date() #88473
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2021-06-04.01:50:33.984>labels= ['extension-modules', '3.11', 'performance']
title='date.today() is half as fast as datetime.now().date()'updated_at=<Date2021-06-04.21:22:03.867>user='https://github.com/asottile'
$ python3.10 -m timeit -s 'from datetime import datetime''datetime.now().date()'500000 loops, best of 5: 708 nsec per loop
$ python3.10 -m timeit -s 'from datetime import date''date.today()'200000 loops, best of 5: 1.4 usec per loop
this surprised me so I dug into it -- it appears a fast path can be added to date.today() to make it faster than datetime.date.now() -- though I'm rather unfamiliar with the functions involved here
here is my ~sloppy patch attempting to add a fast path, I would need some guidance to improve it and get it accepted:
Yeah, I knew this was slower and it's been on my long list to look at it (tied to this is the fact that datetime.today() is basically just a slow version of datetime.now(), in defiance of user expectations).
My inclination is that we shouldn't re-implement fromtimestamp in date.today, but rather call date_fromtimestamp in the fast path. I believe that incurs the overhead of creating one additional Python object (an integer), but if it's a sufficiently significant speedup, we could possibly refactor date_fromtimestamp to a version that accepts a C integer and a version that accepts a Python integer, then call the version accepting a C integer.
I think this won't give any speedup to datetime.today, since datetime.today will still take the slow path. If we care about this, we may be able to implement datetime.today as an alias for datetime.now(None), assuming there are no behavioral differences between the two.
changed the title [-]date.today() is 2x slower than datetime.now().date()[/-][+]date.today() is half as fast as datetime.now().date()[/+]on Jun 4, 2021
changed the title [-]date.today() is 2x slower than datetime.now().date()[/-][+]date.today() is half as fast as datetime.now().date()[/+]on Jun 4, 2021
asottile commented on Jun 4, 2021
asottilemannequin
MannequinAuthor
More actions
@terry.reddy -- I believe your title change makes this more difficult to understand
Activity
asottile commentedon Jun 4, 2021
this surprised me so I dug into it -- it appears a fast path can be added to
date.today()to make it faster thandatetime.date.now()-- though I'm rather unfamiliar with the functions involved herehere is my ~sloppy patch attempting to add a fast path, I would need some guidance to improve it and get it accepted:
after this,
date.today()is faster!\o/
pganssle commentedon Jun 4, 2021
Yeah, I knew this was slower and it's been on my long list to look at it (tied to this is the fact that
datetime.today()is basically just a slow version ofdatetime.now(), in defiance of user expectations).My inclination is that we shouldn't re-implement
fromtimestampindate.today, but rather calldate_fromtimestampin the fast path. I believe that incurs the overhead of creating one additional Python object (an integer), but if it's a sufficiently significant speedup, we could possibly refactordate_fromtimestampto a version that accepts a C integer and a version that accepts a Python integer, then call the version accepting a C integer.I think this won't give any speedup to
datetime.today, sincedatetime.todaywill still take the slow path. If we care about this, we may be able to implementdatetime.todayas an alias fordatetime.now(None), assuming there are no behavioral differences between the two.[-]date.today() is 2x slower than datetime.now().date()[/-][+]date.today() is half as fast as datetime.now().date()[/+][-]date.today() is 2x slower than datetime.now().date()[/-][+]date.today() is half as fast as datetime.now().date()[/+]asottile commentedon Jun 4, 2021
@terry.reddy -- I believe your title change makes this more difficult to understand
asottile commentedon Jun 4, 2021
*terry.reedy oops typo!
2 remaining items
gh-88473: Implement fast path in date.today() for date types (#130980)
StanFromIreland commentedon Sep 16, 2025
@pganssle do we want to backport?
picnixz commentedon Sep 16, 2025
Performance improvements are usually not backported if they are considered new features. I would see this as a feature rather than a bugfix.
StanFromIreland commentedon Sep 16, 2025
That was my thinking.
AskicV commentedon May 14, 2026
Interesting, the issue still exist with CPython 3.14.4:
python -m timeit -s 'from datetime import datetime' 'datetime.now().date()'Provides the following outpt:
500000 loops, best of 5: 416 nsec per loopWhile this code:
python -m timeit -s 'from datetime import date' 'date.today()'Gives the following output:
500000 loops, best of 5: 834 nsec per loopSo the ratio still holds.
StanFromIreland commentedon May 14, 2026
That's because it didn't make it into 3.14, in 3.15/6:
AskicV commentedon May 14, 2026
Thanks Stan, I missed that.