-
-
Notifications
You must be signed in to change notification settings - Fork 31.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
date.today() is half as fast as datetime.now().date() #88473
Comments
$ 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 here is my ~sloppy patch attempting to add a fast path, I would need some guidance to improve it and get it accepted: $ git diff -w
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
index 8ef2dad37a..7eaa5d1740 100644
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -2875,6 +2875,17 @@ date_fromtimestamp(PyObject *cls, PyObject *obj)
static PyObject *
date_today(PyObject *cls, PyObject *dummy)
{
+ /* fast path, don't call fromtimestamp */
+ if ((PyTypeObject *)cls == &PyDateTime_DateType) {
+ struct tm tm;
+ time_t t;
+ time(&t);
+ localtime_r(&t, &tm);
+ return new_date_ex(tm.tm_year + 1900,
+ tm.tm_mon + 1,
+ tm.tm_mday,
+ (PyTypeObject*)cls);
+ } else {
PyObject *time;
PyObject *result;
_Py_IDENTIFIER(fromtimestamp);
@@ -2893,6 +2904,7 @@ date_today(PyObject *cls, PyObject *dummy)
Py_DECREF(time);
return result;
}
+}
/*[clinic input]
@classmethod after this, $ ./python -m timeit -s 'from datetime import datetime' 'datetime.now().date()'
500000 loops, best of 5: 764 nsec per loop
$ ./python -m timeit -s 'from datetime import date' 'date.today()'
500000 loops, best of 5: 407 nsec per loop \o/ |
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 My inclination is that we shouldn't re-implement I think this won't give any speedup to |
@terry.reddy -- I believe your title change makes this more difficult to understand |
*terry.reedy oops typo! |
@pganssle, @iritkatriel - Is it possible that we can review the date.today()
# datetime.date(2022, 12, 17)
# Then datetime.today() should be:
datetime.today()
# datetime.datetime(2022, 12, 17, 0, 0, 0, 0) I understand that this is a widely used method and may cause lots of disruption. But maybe we can add a legacy flag? # Legacy:
datetime.today(legacy=True) # true by default.
# datetime.datetime(2022, 12, 17, 9, 37, 30, 653381)
# Non-legacy:
datetime.today(legacy=False)
# datetime.datetime(2022, 12, 17, 0, 0, 0, 0) |
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:
bugs.python.org fields:
Linked PRs
The text was updated successfully, but these errors were encountered: