gpt4 book ai didi

python - 为什么 `datetime.date.today` 在 Python 中比 `datetime.datetime.now` 慢?

转载 作者:太空宇宙 更新时间:2023-11-03 12:11:06 24 4
gpt4 key购买 nike

出于好奇,我对这两个函数做了如下基准测试:

In [12]: %timeit datetime.datetime.now()
100000 loops, best of 3: 5.09 µs per loop

In [13]: %timeit datetime.date.today()
100000 loops, best of 3: 6.4 µs per loop

我以为date对象包含的信息较少,所以它应该是更快的,但事实证明它更慢。

可能是什么原因?

最佳答案

我今天被这个搞得一头雾水,所以我会告诉你我发现了什么——系好安全带。

首先,date.today() 的实现必须通过成员函数调用——这个查找似乎是最慢的部分:

https://github.com/python/cpython/blob/b2bf2bc1ece673d387341e06c8d3c2bc6e259747/Modules/_datetimemodule.c#L2886-L2892

摘录于此:

static PyObject *
date_today(PyObject *cls, PyObject *dummy)
{
PyObject *time;
PyObject *result;
_Py_IDENTIFIER(fromtimestamp);

time = time_time();
if (time == NULL)
return NULL;

/* Note well: today() is a class method, so this may not call
* date.fromtimestamp. For example, it may call
* datetime.fromtimestamp. That's why we need all the accuracy
* time.time() delivers; if someone were gonzo about optimization,
* date.today() could get away with plain C time().
*/
result = _PyObject_CallMethodIdOneArg(cls, &PyId_fromtimestamp, time);
Py_DECREF(time);
return result;
}

值得注意的是它总是通过慢速路径

所以我想,为什么不给它一条快速路径呢?

$ 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

缩放缩放

$ ./python -m timeit -s 'from datetime import date' 'date.today()'
500000 loops, best of 5: 407 nsec per loop
$ ./python -m timeit -s 'from datetime import datetime' 'datetime.now().date()'
500000 loops, best of 5: 764 nsec per loop

关于python - 为什么 `datetime.date.today` 在 Python 中比 `datetime.datetime.now` 慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25805590/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com