Skip to content

Tips_CPUUsage

Ross Bencina edited this page Mar 24, 2022 · 3 revisions

Tips - CPU Usage

In many applications, it is important to keep the CPU usage to a minimum. Minimizing CPU usage may conflict with other objectives, such as minimizing latency, so it is up to you to decide which of these suggestions to use.

Here are some tips for things that might improve CPU performance:

  • Use a high latency: Try using a large latency, which keeps lots of audio data buffered, so that the OS doesn't need to constantly switch your audio app in and out of context. Of course, using too high a latency might cause cache misses, so you'll have to experiment to see what's best for your app.

  • Use a large buffer size: If you are using small buffer sizes, try something larger. Most platforms do well with something in the 128-1024 range, and powers of two usually work well. Often, using paFramesPerBufferUnspecified will result in both good CPU performance and low latency, though this depends on the platform.

  • Try platform specific flags: Some platforms offer special include files that offer platform specific features. For example, on Mac OS X, you can use the functions and constants in pa_mac_core.h to adjust sample-rate conversion settings and other things.

Denormal Number Performance Issue

When implementing recursive (IIR) filters using floating point arithmetic, silent (zero) input can cause filter state variables to decay into the denormal range. On some CPUs, such as x86 and some ARM CPUs, denormal calculations are many times slower than normal-range computations (see https://en.wikipedia.org/wiki/Subnormal_number#Performance_issues). This can cause CPU load to drastically increase.

On some CPUs it is possible to disable denormal numbers (e.g. using the _mm_setcsr x86 instruction) as discussed here: https://github.com/PortAudio/portaudio/issues/427 There are also a range of options for software mitigations, such as mixing in very small pulses or noise, or periodically checking and clamping small values to zero. Such techniques are surveyed in Laurent de Soras' white paper "Denormal numbers in floating point signal processing applications" available here: http://ldesoras.free.fr/doc/articles/denormal-en.pdf

Clone this wiki locally