⚡️ Speed up method Styler.set_table_attributes by 8%
#409
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.
📄 8% (0.08x) speedup for
Styler.set_table_attributesinpandas/io/formats/style.py⏱️ Runtime :
13.0 microseconds→12.0 microseconds(best of12runs)📝 Explanation and details
The optimized code reduces redundant
get_option()calls in theStyler.__init__()method by batching configuration lookups.Key optimization: Instead of calling
get_option()individually for each parameter (thousands, decimal, na_rep, escape, formatter) regardless of whether they're already provided, the optimized version:Noneget_option()for missing values rather than unconditionally for all 5 configuration keysget_optionfunction reference asgetoptto avoid repeated global lookupsWhy this is faster:
get_option()involves dictionary lookups in pandas' global configuration system (_global_config) and string pattern matching. The original code always made 5get_option()calls, while the optimized version typically makes fewer calls when some parameters are explicitly provided (common in real usage).Performance impact: The 7% speedup in
set_table_attributes()reflects improvedStylerinitialization efficiency. Sincedf.stylecreates newStylerinstances, this optimization benefits any styling workflow. The test results show consistent 5-20% improvements across various scenarios, with the optimization being most effective when users provide some (but not all) formatting parameters explicitly, reducing unnecessary configuration lookups.This optimization maintains identical behavior while eliminating wasteful global configuration access, making
Stylercreation more efficient for pandas styling operations.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-Styler.set_table_attributes-mj9y6v3xand push.