Chapter 4. API Guides
Tasks If the option CONFIG_FREERTOS_GENERATE_RUN_TIME_STATS is enabled then the FreeRTOS API
vTaskGetRunTimeStats() can be used to retrieve runtime information about the processor time used by each
FreeRTOS task.
SEGGER SystemView is an excellent tool for visualizing task execution and looking for performance issues or im-
provements in the system as a whole.
Improving Overall Speed The following optimizations will improve the execution of nearly all code - including
boot times, throughput, latency, etc:
• Set CONFIG_ESPTOOLPY_FLASHMODE to QIO or QOUT mode (Quad I/O). Both will almost double the
speed at which code is loaded or executed from flash compared to the default DIO mode. QIO is slightly faster
than QOUT if both are supported. Note that both the flash chip model and the electrical connections between
the ESP32-S2 and the flash chip must support quad I/O modes or the SoC will not work correctly.
• Set CONFIG_COMPILER_OPTIMIZATION to“Optimize for performance (-O2)”. This may slightly increase
binary size compared to the default setting, but will almost certainly increase performance of some code. Note
that if your code contains C or C++ Undefined Behaviour then increasing the compiler optimization level may
expose bugs that otherwise are not seen.
• Avoid using floating point arithmetic (float). On ESP32-S2 these calculations are emulated in software and
are very slow. If possible then use fixed point representations, a different method of integer representation, or
convert part of the calculation to be integer only before switching to floating point.
• Avoid using double precision floating point arithmetic (double). These calculations are emulated in software
and are very slow. If possible then use an integer-based representation, or single-precision floating point.
Reduce Logging Overhead Although standard output is buffered, it’s possible for an application to be limited
by the rate at which it can print data to log output once buffers are full. This is particularly relevant for startup time
if a lot of output is logged, but can happen at other times as well. There are multiple ways to solve this problem:
• Reduce the volume of log output by lowering the app CONFIG_LOG_DEFAULT_LEVEL (the equivalent boot-
loader setting is CONFIG_BOOTLOADER_LOG_LEVEL). This also reduces the binary size, and saves some
CPU time spent on string formatting.
• Increase the speed of logging output by increasing the CONFIG_ESP_CONSOLE_UART_BAUDRATE. (Un-
less using internal USB-CDC for serial console, in which case the serial throughput doesn’t depend on the
configured baud rate.)
Not Recommended The following options will also increase execution speed, but are not recommended as they
also reduce the debuggability of the firmware application and may increase the severity of any bugs.
• Set CONFIG_COMPILER_OPTIMIZATION_ASSERTION_LEVEL to disabled. This also reduces firmware bi-
nary size by a small amount. However, it may increase the severity of bugs in the firmware including security-
related bugs. If necessary to do this to optimize a particular function, consider adding #define NDEBUG
in the top of that single source file instead.
Targeted Optimizations The following changes will increase the speed of a chosen part of the firmware applica-
tion:
• Move frequently executed code to IRAM. By default, all code in the app is executed from flash cache. This
means that it’s possible for the CPU to have to wait on a“cache miss”while the next instructions are loaded
from flash. Functions which are copied into IRAM are loaded once at boot time, and then will always execute
at full speed.
IRAM is a limited resource, and using more IRAM may reduce available DRAM, so a strategic approach is
needed when moving code to IRAM. See IRAM (Instruction RAM) for more information.
• Jump table optimizations can be re-enabled for individual source files that don’t need to be placed in IRAM. For
hot paths in large switch cases this will improve performance. For instructions on how to add the -fjump-tables -
ftree-switch-conversion options when compiling individual source files, see Controlling Component Compilation
Espressif Systems 1441
Submit Document Feedback
Release v4.4