Optimized interpolate
and bezier
in manim.utils.bezier
#3960
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.
Overview: What does this pull request change?
Slightly optimized
interpolate
andbezier
inmanim.utils.bezier
.Related PR: #3281
interpolate
Rewriting this function to use 1 product instead of 2, makes a speedup of around 30%:
bezier
First of all, I added the cases for Bézier curves of grade 0 and 1. This prevents them from falling back to the expensive general nth grade case:
Then, I made a very slight optimization for the cubic Bézier case:
It reported a small speedup of around 10% - 15%. Not too bad! I did something similar with the quadratic Bézier:
Finally, the general nth grade Bézier case. I avoided the extensive use of the
choose
function which can be expensive, and instead used the recursive definition of Béziers I used for functions likepartial_bezier_points
andsubdivide_bezier
.Also,
bezier
now returns named functions instead of lambdas, which is good for debugging and profiling: now you can know which function is taking what part of the time when profiling your code.Links to added or changed documentation pages
bezier
https://manimce--3960.org.readthedocs.build/en/3960/reference/manim.utils.bezier.html#manim.utils.bezier.bezier
interpolate
https://manimce--3960.org.readthedocs.build/en/3960/reference/manim.utils.bezier.html#manim.utils.bezier.interpolate
Reviewer Checklist