You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As an example, you may optimize a chunk of code by caching intermediate values.
34
31
However, if that code was slow due to memory constraints, caching the values and
35
32
reading them later may be even slower than calculating them from scratch!
@@ -96,6 +93,50 @@ Once you have your baseline profile/benchmark, make your changes and rebuild the
96
93
engine with the exact same build settings you used before. Then profile again
97
94
and compare the results.
98
95
96
+
Tools for optimization
97
+
~~~~~~~~~~~~~~~~~~~~~~
98
+
99
+
Profilers
100
+
^^^^^^^^^
101
+
102
+
Profilers are the most important tool for everyone optimizing code. They show you which
103
+
parts of the code are responsible for slow execution or heavy CPU load. Profilers are
104
+
therefore excellent for identifying what needs to be optimized, and to test whether
105
+
performance was improved after making changes. Godot has a built-in profiler, but it
106
+
does not provide very detailed information. Instead, use dedicated C++ profilers, which are
107
+
`explained in the Godot documentation <https://docs.godotengine.org/en/stable/engine_details/development/debugging/using_cpp_profilers.html>`__.
108
+
109
+
Benchmarks
110
+
^^^^^^^^^^
111
+
112
+
Benchmarks can be a great and simple tool to test the impact of your changes
113
+
of an isolated piece of code. However, they can be misleading because it's easy
114
+
to write them in a way that doesn't reflect real-world usage. When using
115
+
benchmarks to test the performance of your code, always be aware of their
116
+
potential caveats, and familiarize yourself with good benchmark practices.
117
+
118
+
To start writing benchmarks in Godot, you can use the following code templates:
119
+
120
+
.. tabs::
121
+
.. code-tab:: gdscript GDScript
122
+
123
+
var start = Time.get_ticks_msec()
124
+
var s := "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
125
+
for i in range(10000):
126
+
s.replace("e", "b") # Benchmarks the 'replace' function.
127
+
print(Time.get_ticks_msec() - start, "ms")
128
+
129
+
.. code-tab:: cpp
130
+
131
+
String s = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
132
+
133
+
auto t0 = std::chrono::high_resolution_clock::now();
134
+
for (int i = 0; i < 100000; i ++) {
135
+
String s1 = s.replace("e", "b"); // Benchmarks the 'replace' function.
136
+
}
137
+
auto t1 = std::chrono::high_resolution_clock::now();
0 commit comments