Performance tuning tips

AmiBroker is one of the fastest (if not the fastest) technical analysis programs. Still, the user, by applying incorrect settings, poor formula coding, and other suboptimal choices, can significantly slow it down. In this short chapter, we will give a few hints on how to make the program perform as it was originally designed to.

There are three areas of performance tuning:

  1. Operating system / machine level
  2. AmiBroker settings level
  3. Formula coding

At the operating system level, you should do the following:

At the AmiBroker settings level

At the formula coding level

Poor formula coding is the foremost reason for slowdown. People coming from "other" languages often do not realize the full potential of AFL array processing and code everything in the "old" style (i.e., with loops). Loops can be 10-50 times slower than equivalent array-based code. So, for best speed, you should avoid loops altogether, replacing them with array processing, or at least make looping code as short as possible.

Consider the following code:

SetBarsRequired( sbrAll );
GetPerformanceCounter(1);

for( i = 0; i<BarCount; i++)
{
   med[ i ] = (
H[ i ] + L[ i ])/2;
}

"Loop time: "+GetPerformanceCounter(1);

med = (
H + L )/2;

"Array time: "+GetPerformanceCounter(1);

When running this code on 350,000 bars, the loop version takes 100ms (0.1s) to execute, and only 2ms (0.002s) to execute the array version (so array code is 50 times faster than looping). The difference is not as significant with fewer bars, but even with 300 bars, the loop requires 0.1ms and the array needs 0.01ms, so it is 10 times faster.

So, there are a few guidelines for AFL coding:

Performance monitoring

In order to help you with real-time monitoring of program performance, AmiBroker provides two tools. First is the Performance monitor window; the second is the Performance indicator, which is located on the rightmost side of the AmiBroker status bar.

The status bar performance indicator shows:

Load Factor is a percentage value that shows the relative 'snappiness' of the program. The load factor is calculated as (total chart refresh time in milliseconds)/2 + (total data access time in milliseconds)/2 + (free virtual memory below 20% of total memory available). So it will reach 100% if any of the situations listed below occur:

Total chart refresh time is the sum of times needed to completely redraw all charts displayed on screen; it includes AFL execution time for each chart pane and GDI (graphics) output on screen. (It does not include data access.)

Total data access time is the sum of times required to access fresh data via the plugin for all displayed symbols, plus the time required to apply time filtering and time compression from the base interval to the displayed interval.

Plug-in time per symbol is the time spent in the plug-in GetQuotes call per *single* symbol. If you display 10 symbol charts at once, AmiBroker will call GetQuotes 10 times, so this time gets multiplied by the number of symbols displayed (this total plugin time *is* included in the total data access time figure - listed above).
If Plug-in time per symbol exceeds 10ms, it means that the plugin is slow (or does not use the new ADK 2.0), if this is the case, you should contact the plugin vendor to get an updated plugin that uses ADK 2.0.

It is recommended to keep this load factor below 100%. When the load factor is 100%, AmiBroker is able to keep updating all charts in real-time (more frequently than 5 times per second) and maintain a responsive and smooth user interface. With a load factor of 200%, AmiBroker is still able to keep updating all charts as frequently as 2.5 times per second, but user interface reaction time may be impaired a bit. Keeping the load at 100% or less is recommended.
200% is the maximum value that allows more or less "normal" operation.

When the load factor rises above 100%, a warning tooltip will pop up once, informing what the reason for poor performance is. When the load factor rises above 300%, the above tooltip will reappear every minute.

AmiBroker will continue working with loads even above 1000%; however, the performance will be bad (one update per 5 seconds or more).

Multithreading performance

For more information about the optimum use of multithreading and other general remarks regarding performance, see Efficient Use of Multithreading.