⚡️ Speed up function validate_gantt by 58x
#5386
Open
+261
−3
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.
📄 5,759% (57.59x) speedup for
validate_ganttinplotly/figure_factory/_gantt.py⏱️ Runtime :
154 milliseconds→2.63 milliseconds(best of246runs)📝 Explanation and details
The optimization achieves a 58x speedup by eliminating the major performance bottleneck in pandas DataFrame processing.
Key optimizations:
Pre-fetch column data as numpy arrays: The original code used
df.iloc[index][key]for each cell access, which triggers pandas' slow row-based indexing mechanism. The optimized version extracts all column data upfront usingdf[key].valuesand stores it in a dictionary, then uses direct numpy array indexingcolumns[key][index]inside the loop.Use actual DataFrame columns: Instead of iterating over the DataFrame object itself (which includes metadata), the code now uses
list(df.columns)to get only the actual column names.Why this is dramatically faster:
df.iloc[index][key]creates temporary pandas Series objects and involves complex indexing logic for each cellcolumns[key][index]is orders of magnitude fasterdf.ilocline consumed 96.8% of execution time (523ms), while the optimized dictionary comprehension takes only 44.9% (4.2ms)Performance characteristics:
This optimization is most beneficial for DataFrame inputs with many rows, where the repeated
iloccalls created a severe performance bottleneck.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-validate_gantt-mhcxyu68and push.