Skip to content

Commit 53a9b6f

Browse files
committed
gh-112919: Speed-up datetime, date and time.replace()
Use argument clinic and call new_* functions directly. This speeds up these functions 6x to 7.5x when calling with keyword arguments. Before: $ python -m timeit -s "from datetime import datetime; dt = datetime.now()" "dt.replace(microsecond=0)" 500000 loops, best of 5: 501 nsec per loop $ python -m timeit -s "from datetime import date; d = date.today()" "d.replace(day=1)" 1000000 loops, best of 5: 273 nsec per loop $ python -m timeit -s "from datetime import time; t = time()" "t.replace(microsecond=0)" 1000000 loops, best of 5: 338 nsec per loop After: $ python -m timeit -s "from datetime import datetime; dt = datetime.now()" "dt.replace(microsecond=0)" 5000000 loops, best of 5: 65.9 nsec per loop $ python -m timeit -s "from datetime import date; d = date.today()" "d.replace(day=1)" 5000000 loops, best of 5: 45.6 nsec per loop $ python -m timeit -s "from datetime import time; t = time()" "t.replace(microsecond=0)" 5000000 loops, best of 5: 51 nsec per loop
1 parent 28b2b74 commit 53a9b6f

File tree

7 files changed

+476
-96
lines changed

7 files changed

+476
-96
lines changed

Include/internal/pycore_global_objects_fini_generated.h

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_global_strings.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ struct _Py_global_strings {
362362
STRUCT_FOR_ID(d)
363363
STRUCT_FOR_ID(data)
364364
STRUCT_FOR_ID(database)
365+
STRUCT_FOR_ID(day)
365366
STRUCT_FOR_ID(decode)
366367
STRUCT_FOR_ID(decoder)
367368
STRUCT_FOR_ID(default)
@@ -429,6 +430,7 @@ struct _Py_global_strings {
429430
STRUCT_FOR_ID(fix_imports)
430431
STRUCT_FOR_ID(flags)
431432
STRUCT_FOR_ID(flush)
433+
STRUCT_FOR_ID(fold)
432434
STRUCT_FOR_ID(follow_symlinks)
433435
STRUCT_FOR_ID(format)
434436
STRUCT_FOR_ID(from_param)
@@ -459,6 +461,7 @@ struct _Py_global_strings {
459461
STRUCT_FOR_ID(headers)
460462
STRUCT_FOR_ID(hi)
461463
STRUCT_FOR_ID(hook)
464+
STRUCT_FOR_ID(hour)
462465
STRUCT_FOR_ID(id)
463466
STRUCT_FOR_ID(ident)
464467
STRUCT_FOR_ID(identity_hint)
@@ -541,11 +544,14 @@ struct _Py_global_strings {
541544
STRUCT_FOR_ID(metaclass)
542545
STRUCT_FOR_ID(metadata)
543546
STRUCT_FOR_ID(method)
547+
STRUCT_FOR_ID(microsecond)
548+
STRUCT_FOR_ID(minute)
544549
STRUCT_FOR_ID(mod)
545550
STRUCT_FOR_ID(mode)
546551
STRUCT_FOR_ID(module)
547552
STRUCT_FOR_ID(module_globals)
548553
STRUCT_FOR_ID(modules)
554+
STRUCT_FOR_ID(month)
549555
STRUCT_FOR_ID(mro)
550556
STRUCT_FOR_ID(msg)
551557
STRUCT_FOR_ID(mycmp)
@@ -650,6 +656,7 @@ struct _Py_global_strings {
650656
STRUCT_FOR_ID(salt)
651657
STRUCT_FOR_ID(sched_priority)
652658
STRUCT_FOR_ID(scheduler)
659+
STRUCT_FOR_ID(second)
653660
STRUCT_FOR_ID(seek)
654661
STRUCT_FOR_ID(seekable)
655662
STRUCT_FOR_ID(selectors)
@@ -725,6 +732,7 @@ struct _Py_global_strings {
725732
STRUCT_FOR_ID(type)
726733
STRUCT_FOR_ID(type_params)
727734
STRUCT_FOR_ID(tz)
735+
STRUCT_FOR_ID(tzinfo)
728736
STRUCT_FOR_ID(tzname)
729737
STRUCT_FOR_ID(uid)
730738
STRUCT_FOR_ID(unlink)

Include/internal/pycore_runtime_init_generated.h

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Include/internal/pycore_unicodeobject_generated.h

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Speed-up :func:`datetime.datetime.replace`, :func:`datetime.date.replace` and
2+
:func:`datetime.time.replace`.

0 commit comments

Comments
 (0)