# Bug report ### Bug description: datetime.replace() is very slow when called with keyword arguments. For example, with python 3.13 from main on my laptop ```console $ python -m timeit -s "from datetime import datetime; dt = datetime.now()" "dt.replace(microsecond=0)" 500000 loops, best of 5: 501 nsec per loop ``` For comparison, ```console $ python -m timeit -s "from datetime import datetime" "datetime.now()" 2000000 loops, best of 5: 150 nsec per loop $ python -m timeit -s "from datetime import datetime" "datetime(2020, 1, 1, 12, 34, 56, 123456)" 2000000 loops, best of 5: 119 nsec per loop ``` So calling replace() is over 4x slower than constructing a new datetime from components and over 3x slower than now(), which makes a system call. ### CPython versions tested on: 3.9, 3.10, 3.13 ### Operating systems tested on: Linux <!-- gh-linked-prs --> ### Linked PRs * gh-112921 * gh-115344 <!-- /gh-linked-prs -->
Activity
pythongh-112919: Speed-up date, time and datetime.replace()
pythongh-112919: Speed-up date, time and datetime.replace()
pythongh-112919: Speed-up datetime, date and time.replace()
pythongh-112919: Speed-up datetime, date and time.replace()
pythongh-112919: Speed-up datetime, date and time.replace()
pythongh-112919: Speed-up datetime, date and time.replace()
gh-112919: Speed-up datetime, date and time.replace() (GH-112921)
serhiy-storchaka commentedon Jan 30, 2024
Thank you for your contribution @eltoder.
I reclassified it as a new feature. It was not that
replace()methods were too slow, it was that constructors were too fast.pythongh-112919: Speed-up datetime, date and time.replace() (pythonGH…
pythongh-112919: Speed-up datetime, date and time.replace() (pythonGH…
[3.12] pythongh-112919: Speed-up datetime, date and time.replace() (p…