Skip to content

Conversation

StanFromIreland
Copy link
Member

@StanFromIreland StanFromIreland commented Mar 8, 2025

Other suggested implementations are not as backward compatible.

$ ./python -m timeit -s 'from datetime import date' 'date.today()'
1000000 loops, best of 5: 271 nsec per loop
$ python3.14 -m timeit -s 'from datetime import date' 'date.today()'
200000 loops, best of 5: 1.36 usec per loop

~5x faster for date types

@picnixz
Copy link
Member

picnixz commented Mar 8, 2025

Are the benchmarks realised on a PGO build or a debug build? or maybe PGO+LTO? or just release build?

@StanFromIreland
Copy link
Member Author

Both are just plain builds. python3.14 is from the morning but I doubt the few commits matter.

@picnixz
Copy link
Member

picnixz commented Mar 8, 2025

By plain builds, do you mean with or withou --with-pydebug? I assume without so it's probably release builds so it should be fine. But please the configure flags

@StanFromIreland StanFromIreland requested a review from picnixz March 8, 2025 22:06
@StanFromIreland
Copy link
Member Author

@pganssle can you take a look at this?

@StanFromIreland StanFromIreland requested a review from picnixz March 12, 2025 18:27
Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On my part, it looks good but I'll let a datetime expert have the final decision.

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
@StanFromIreland
Copy link
Member Author

Friendly ping @pganssle Any comments on this?

@StanFromIreland
Copy link
Member Author

Friendly ping @pganssle :-)

Copy link
Member

@pganssle pganssle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an acceptable way to do it, but IIRC we already pushed some of this kind of dispatching logic in new_date_subclass_ex, so I think you can replace the entire contents of date_today with this:

static PyObject *
date_today(PyObject *cls, PyObject *Py_UNUSED(dummy))
{
    /* Use C implementation to boost performance for date type */
    struct tm tm;
    time_t t;
    time(&t);

    if (_PyTime_localtime(t, &tm) != 0) {
        return NULL;
    }

    return new_date_subclass_ex(tm.tm_year + 1900,
                       tm.tm_mon + 1,
                       tm.tm_mday,
                       (PyObject *)cls);
}

And you'll get the same speedup and as a bonus datetime.today() will also get the same speedup.

@bedevere-app
Copy link

bedevere-app bot commented Sep 15, 2025

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

# Conflicts:
#	Modules/_datetimemodule.c
@StanFromIreland
Copy link
Member Author

And you'll get the same speedup and as a bonus datetime.today() will also get the same speedup.

It will be incorrect though? Since datetime.today has hours, minutes etc.

@pganssle
Copy link
Member

It will be incorrect though? Since datetime.today has hours, minutes etc.

Hah, oops, right, right. Should have run the test suite and not just the benchmark.

OK, let's go with this as it is and if we want to speed up datetime.today we can add an implementation for datetime.datetime that is just effectively return datetime.now(tz=None).

@pganssle pganssle merged commit fc3e22a into python:main Sep 16, 2025
48 of 49 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants