Small speed up to frequently called datetime functions#85399
Merged
Conversation
We can reduce some python overhead and make functions with static arguments ~30% faster by wrapping them as a partial which avoids the overhead of constructing the underlying call every time since it will only be done once https://github.com/python/cpython/blob/4b4e6da7b53f9eb2d3654d321f6acfbd599bd990/Modules/_functoolsmodule.c#L286 ``` % cat timer.py import timeit print(timeit.timeit("utcnow()", setup="from tester import utcnow")) print(timeit.timeit("utcnow_partial()", setup="from tester import utcnow_partial")) % python3 timer.py 0.207963207969442 0.14283533301204443 % cat tester.py from functools import partial import datetime as dt UTC = dt.timezone.utc def utcnow() -> dt.datetime: """Get now in UTC time.""" return dt.datetime.now(UTC) utcnow_partial = partial(dt.datetime.now, UTC) ```
Member
Author
|
Its hitting the event rearm |
elupus
reviewed
Jan 8, 2023
elupus
approved these changes
Jan 8, 2023
Member
Author
|
Thanks |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed change
We call time functions as much as 10000-20000 times per minute so any savings here makes a difference with latency (these are mostly in the bluetooth advertisement path).
We can reduce some python overhead and make functions with static arguments ~30% faster by wrapping them as a
partialwhich avoids the overhead of constructing the underlying call every time since it will only be done oncehttps://github.com/python/cpython/blob/4b4e6da7b53f9eb2d3654d321f6acfbd599bd990/Modules/_functoolsmodule.c#L286
Type of change
Additional information
Checklist
black --fast homeassistant tests)If user exposed functionality or configuration variables are added/changed:
If the code communicates with devices, web services, or third-party tools:
Updated and included derived files by running:
python3 -m script.hassfest.requirements_all.txt.Updated by running
python3 -m script.gen_requirements_all..coveragerc.To help with the load of incoming pull requests: