Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Added guide on profiling and improving performance (#2517)

Darylgolden859c1793f607
diff --git a/docs/source/_static/snakeviz.png b/docs/source/_static/snakeviz.png
new file mode 100644
index 000000000..b4b7625c5
Binary files /dev/null and b/docs/source/_static/snakeviz.png differ
diff --git a/docs/source/contributing.rst b/docs/source/contributing.rst
index 08bbd3132..79408d984 100644
--- a/docs/source/contributing.rst
+++ b/docs/source/contributing.rst
@@ -44,4 +44,5 @@ Contributing can be confusing, so here are a few guides:
contributing/typings
contributing/admonitions
contributing/testing
+ contributing/performance
contributing/internationalization
diff --git a/docs/source/contributing/performance.rst b/docs/source/contributing/performance.rst
new file mode 100644
index 000000000..f16121c40
--- /dev/null
+++ b/docs/source/contributing/performance.rst
@@ -0,0 +1,81 @@
+=====================
+Improving performance
+=====================
+
+One of Manim's main flaws as an animation library is its slow performance.
+As of time of writing (January 2022), the library is still very unoptimized.
+As such, we highly encourage contributors to help out in optimizing the code.
+
+Profiling
+=========
+
+Before the library can be optimized, we first need to identify the bottlenecks
+in performance via profiling. There are numerous Python profilers available for
+this purpose; some examples include cProfile and Scalene.
+
+Running an animation as a script
+--------------------------------
+
+Most instructions for profilers assume you can run the python file directly as a
+script from the command line. While Manim animations are usually run from the
+command-line, we can run them as scripts by adding something like the following
+to the bottom of the file:
+
+.. code-block:: python
+
+ with tempconfig({"quality": "medium_quality", "disable_caching": True}):
+ scene = SceneName()
+ scene.render()
+
+Where ``SceneName`` is the name of the scene you want to run. You can then run the
+file directly, and can thus follow the instructions for most profilers.
+
+An example: profiling with cProfile and SnakeViz
+-------------------------------------------------
+
+Install SnakeViz:
+
+.. code-block:: bash
+
+ pip install snakeviz
+
+cProfile is included with in Python's standard library and does not need to be installed.
+
+Suppose we want to profile ``SquareToCircle``. Then we add and save the following code
+to ``square_to_circle.py``:
+
+.. code-block:: python
+
+ from manim import *
+
+
+ class SquareToCircle(Scene):
+ def construct(self):
+ s = Square()
+ c = Circle()
+ self.add(s)
+ self.play(Transform(s, c))
+
+
+ with tempconfig({"quality": "medium_quality", "disable_caching": True}):
+ scene = SquareToCircle()
+ scene.render()
+
+Now run the following in the terminal:
+
+.. code-block:: bash
+
+ python -m cProfile -o square_to_circle.txt square_to_circle.py
+
+This will create a file called ``square_to_circle.txt``.
+
+Now, we can run SnakeViz on the profile file:
+
+.. code-block:: bash
+
+ snakeviz square_to_circle.txt
+
+A browser window or tab will open with a visualization of the profile, which should
+look something like this:
+
+.. image:: /_static/snakeviz.png