使用 C++ 性能分析器
要优化 Godot 的性能,你首先得知道要优化什么,性能分析器在这方面非常有用。
备注
There is a built-in GDScript profiler in the editor, but using a C++ profiler may be useful in cases where the GDScript profiler is not accurate enough or is missing information due to bugs in the profiler.
There are two main types of profilers: sampling profilers and tracing profilers.
Sampling profilers periodically interrupt the running program and take a "sample", which records which functions are running. Using this information, the profiler estimates which functions the program spent the most time in.
Tracing profilers work by recording application-specific events (such as the start and end of a single frame), producing a log called a "trace". The profiler can use the trace to produce a graph showing an accurate high-level timeline of what happened. However, any code that is not explicitly instrumented will not appear in a tracing profiler's timeline!
Godot supports both sampling profilers and tracing profilers, and already includes the logging code for common Godot events for use with a tracing profiler!
Different problems may be easier to debug with one kind of profiler over the other, but it's difficult to provide a set of rules for which to use. Give both a try, and see what you can learn from them!
Sampling profilers
We recommend the following sampling profilers:
VerySleepy (Windows only)
Hotspot (Linux only)
Instruments (Apple only)
这些性能分析器可能不是最强大或者最灵活的,但它们独立的操作和有限的功能非常易用。
设置 Godot
如果要获取有用的性能分析信息,就必须使用包含调试符号的 Godot 构建。官方二进制文件并不包含调试符号,因为会显著增加文件下载大小。
To get profiling data that best matches the production environment (but with debugging symbols),
you should compile binaries with the production=yes debug_symbols=yes SCons options.
It is possible to run a profiler on less optimized builds (e.g. target=template_debug without LTO),
but results will naturally be less representative of real world conditions.
警告
请勿在编译完成后使用 strip 命令剥离调试符号,否则运行性能分析器时无法得到有用的信息。
测量启动/关闭耗时
If you're looking into optimizing Godot's startup/shutdown performance,
you can tell the profiler to use the --quit command line option on the Godot binary.
This will exit Godot just after it's done starting.
The --quit option works with --editor, --project-manager, and
--path <path to project directory> (which runs a project directly).
参见
更多 Godot 支持的命令行参数见 命令行教程。
Tracing profilers
Godot currently supports three tracing profilers:
Instruments (Apple only)
In order to use either of them, you'll need to build the engine from source.
If you've never done this before, please read
these docs for the platform you want to profile on.
You'll need to perform the same steps here, but with some additional arguments
for scons.