Profiling means collecting program parameters during its execution. During a profiling, the execution time and the number of calls of individual functions and program code lines are measured. With this tool, the programmer is able to find and optimize the slowest code sections.
Profiling can be performed on the normal chart of the trading platform, as well as using history data in the Strategy Tester. In the first case, a program is launched on a chart that is updated in real time. You can check the program behavior in real conditions. In the second case, the program is launched in the Strategy Tester in the visual mode. The advantage of this method is that you do not need to wait for real data from a trade server or occurrence of certain trading conditions.
Many programs, especially indicators, only perform calculations at the arrival of a new tick (OnTick, OnCalculate). Thus, in order to evaluate performance, you have to wait for new ticks in real time. When profiling using history data, you can immediately provide the required load and test the program performance even on weekends when markets are closed.
The "Sampling" method is used for profiling. The profiler pauses the operation of an MQL program (~1000 times per second) and collects statistics on how many times a pause occurred in a particular code part. This includes analyzing call stacks to determine the "contribution" of each function to the total code execution time. At the end of profiling, you receive information about how many times execution was paused and how many times each of the functions appeared on the call stack:
Sampling is a lightweight and accurate method. Unlike other methods, sampling does not make any changes to the analyzed code, which could affect its running speed.
Be default a program is run on the current open chart for profiling. If profiling is performed on historical data, the current settings of the strategy tester are used. You can use MetaEditor Options to set specify any other chart or to redefine some of the tester settings.
The same setting section allows enabling or disabling function inlining during compilation. During inlining, the function code is added directly to call its call site, which enables significant program acceleration in come cases. However, this procedure makes the profiling of functions difficult. In order to obtain a report on "pure" functions, you can disable inlining.
This option only disables explicit inlining. The functions which are implicitly generated by the compiler may still be used. Such functions are displayed with the [inlined] prefix.
Code optimization mode can be disabled in order to include more details in the profiling report. Code speed without optimization can be several times slower, but this mode provides a wider code coverage. Please note that code bottlenecks can be imprecise without optimization.
The optimization management option is also available under project settings.
After that, a special program version for profiling will be compiled automatically. Depending on the selected profiling type, the program will be launched on a normal chart in the trading platform or in the Strategy Tester.
After the launch, work with the application for some time to try all its features to the maximum. This is necessary in order for the profiler to measure the execution time of all program's functions and lines.
For more accurate profiling results, it is recommended to manually remove the program from the chart instead of using the Stop Profiling command.
After profiling is finished, the results are displayed in the Profiler tab of the Toolbox window. The results are additionally displayed directly in the code: lines with the appropriate functions are highlighted. The brighter the highlighting, the longer it took to complete the function. This features enables a quick and visual spotting of program bottlenecks.
The profiling report is presented as functions or program lines, for each of which there are two indicators available:
The value is displayed as an absolute quantity and as a percentage of the total quantity.
By default, the list shows large functions located at the top levels. Double-click on the line to switch to smaller functions.
The profiling report can be viewed in two modes: by calls and by lines. The second method allows exploring code with maximum detail and to identify not only the slowest functions, but also the slowest parts of such functions. Use the context menu to switch between modes.
For convenience, various elements of the MQL language are displayed as icons in the report:
In addition to these types of functions, the profiler displays:
To view the line number and path to the file where the function is located, hover the mouse cursor over the appropriate line in the report. To view a function in a file, double-click on it.
The profiler displays not all functions but only the ones called during the program operation.
The following commands are available in the context menu: