-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(python): 10% speedup for to_dicts
method
#6415
Conversation
@alexander-beedie Just for my understanding, what is the purpose of doing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually shouldn't we use rows
instead of iterrows
?
I think we might be able to remove this method, as df.rows(named=True)
offers the same functionality after 0.16.0
?
As the size of the frame gets very large it's actually more efficient and, perhaps surprisingly, slightly faster to use df = pl.DataFrame(
{
"x": range(10_000_000),
"y": date(2023,1,1),
"z": "abcdefghijklm",
}
)
with Timer():
_ = df.rows()
# Elapsed time: 16.1816 seconds
with Timer():
_ = list(df.iterrows())
# Elapsed time: 15.5870 seconds
I'd be in favour of even going one step beyond that and removing (I think I can further speed-up both calls too; will experiment...) |
It results in a python bytecode optimisation because we have such a simple/obvious hot loop; when the function gets run it'll use calls to |
Thanks @alexander-beedie for the explanation ! |
Note that we will be able to redirect to
iterrows(named=True)
once0.16.0
is released, as that will build dictionaries using the same optimisations.Until then, here's a simple ~10% speedup on the current approach (as tested on a
-release
build).