8000 gh-112919: Speed-up datetime, date and time.replace() · eltoder/cpython@fff4ae4 · GitHub
[go: up one dir, main page]

Skip to content

Commit fff4ae4

Browse files
committed
pythongh-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 fff4ae4

7 files changed

+494
-86
lines changed

Include/internal/pycore_global_objects_fini_generated.h

Expand all lines: Include/internal/pycore_global_objects_fini_generated.h
Lines changed: 11 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: 11 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)
@@ -401,6 +402,8 @@ struct _Py_global_strings {
401402
STRUCT_FOR_ID(errors)
402403
STRUCT_FOR_ID(event)
403404
STRUCT_FOR_ID(eventmask)
405+
STRUCT_FOR_ID(exc_type)
406+
STRUCT_FOR_ID(exc_value)
404407
STRUCT_FOR_ID(excepthook)
405408
STRUCT_FOR_ID(exception)
406409
STRUCT_FOR_ID(existing_file_name)
@@ -429,6 +432,7 @@ struct _Py_global_strings {
429432
STRUCT_FOR_ID(fix_imports)
430433
STRUCT_FOR_ID(flags)
431434
STRUCT_FOR_ID(flush)
435+
STRUCT_FOR_ID(fold)
432436
STRUCT_FOR_ID(follow_symlinks)
433437
STRUCT_FOR_ID(format)
434438
STRUCT_FOR_ID(from_param)
@@ -459,6 +463,7 @@ struct _Py_global_strings {
459463
STRUCT_FOR_ID(headers)
460464
STRUCT_FOR_ID(hi)
461465
STRUCT_FOR_ID(hook)
466+
STRUCT_FOR_ID(hour)
462467
STRUCT_FOR_ID(id)
463468
STRUCT_FOR_ID(ident)
464469
STRUCT_FOR_ID(identity_hint)
@@ -541,11 +546,14 @@ struct _Py_global_strings {
541546
STRUCT_FOR_ID(metaclass)
542547
STRUCT_FOR_ID(metadata)
543548
STRUCT_FOR_ID(method)
549+
STRUCT_FOR_ID(microsecond)
550+
STRUCT_FOR_ID(minute)
544551
STRUCT_FOR_ID(mod)
545552
STRUCT_FOR_ID(mode)
546553
STRUCT_FOR_ID(module)
547554
STRUCT_FOR_ID(module_globals)
548555
STRUCT_FOR_ID(modules)
556+
STRUCT_FOR_ID(month)
549557
STRUCT_FOR_ID(mro)
550558
STRUCT_FOR_ID(msg)
551559
STRUCT_FOR_ID(mycmp)
@@ -650,6 +658,7 @@ struct _Py_global_strings {
650658
STRUCT_FOR_ID(salt)
651659
STRUCT_FOR_ID(sched_priority)
652660
STRUCT_FOR_ID(scheduler)
661+
STRUCT_FOR_ID(second)
653662
STRUCT_FOR_ID(seek)
654663
STRUCT_FOR_ID(seekable)
655664
STRUCT_FOR_ID(selectors)
@@ -716,6 +725,7 @@ struct _Py_global_strings {
716725
STRUCT_FOR_ID(timetuple)
717726
STRUCT_FOR_ID(top)
718727
STRUCT_FOR_ID(trace_callback)
728+
STRUCT_FOR_ID(traceback)
719729
STRUCT_FOR_ID(trailers)
720730
STRUCT_FOR_ID(translate)
721731
STRUCT_FOR_ID(true)
@@ -725,6 +735,7 @@ struct _Py_global_strings {
725735
STRUCT_FOR_ID(type)
726736
STRUCT_FOR_ID(type_params)
727737
STRUCT_FOR_ID(tz)
738+
STRUCT_FOR_ID(tzinfo)
728739
STRUCT_FOR_ID(tzname)
729740
STRUCT_FOR_ID(uid)
730741
STRUCT_FOR_ID(unlink)

Include/internal/pycore_runtime_init_generated.h

Lines changed: 11 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: 33 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)
0