MetaEditor Help

Code profiling

Code profiling

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.

Launching profiling

Open a program's source code (MQ4 or MQ5). In the Debug menu or Standard toolbar, press "Start profiling on real data Start profiling on real data" or "Start profiling on history data Start profiling on history data".

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 (in the visual mode).

Launching profiling on a chart in the trading platform

By default, an application is launched on EURUSD H1. To launch it on another symbol or period, specify them on the Debug tab in the MetaEditor settings.

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.

Then stop profiling: delete program from the chart or click Stop Profiling Stop Profiling of the Debug menu or in the Standard toolbar.

For more accurate profiling results, it is recommended to manually remove the program from the chart instead of using the Stop Profiling Stop Profiling command.

Viewing the profiling results

After profiling is finished, its results are displayed in the Profiler tab of the Toolbox window. Results can be viewed in two modes: by calls and by lines. Use the context menu to switch between them.

Functions by Calls mode

Profiling results

In this mode, the profiling results are presented by functions:

  • Function – function or class method name.
  • Line – line, in which the function is called. If the function is called in several places in the program, its icon looks like Expand. Clicking on it, opens information for each call.
  • Count – number of function calls during the program operation.
  • Time – function execution time in microseconds, as well as in % relative to the execution time of all functions. The function execution time is the sum of the execution of all functions called inside it.
  • Graph – graph by function execution time.

The icons allow you to specify type of functions:

  • Custom function –  custom function.
  • System function – system function.
  • Event handling function – event handling function (On*).
  • Class method – class method.

In addition to these types of functions, the profiler displays:

  • System function @global_initializations – data on initializing all global variables.
  • System function @global_deinitializations – data on deinitializing all global variables.

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.

Functions by Lines mode

Profiling results

In this mode, the data on the program operation time is represented by lines. Lines are grouped by program functions.

  • Function – function name.
  • Line – number of a line the data is displayed for. To view data on execution time of each function line, click Expand.
  • Count – number of times a code has been executed in a specified line.
  • Time – code execution time in a specified line in microseconds and %. Percentage values for each line are displayed relative to the function execution time. Percentage values for functions are displayed relative to execution time of an entire program.
  • Graph – graph by execution time.

To view a line in a file, double-click on it.

Context menu

The following commands are available in the context menu:

  • Open – go to a line or a function in a source code file. The same can be done by double-clicking or pressing Enter.
  • Expand All – expand all collapsed functions.
  • Collapse All – collapse all expanded functions.
  • Functions by Lines – view the profiling results by lines.
  • Functions by Calls – view the profiling results by calls.
  • Export – export profiling results in Open XML (MS Office Excel), HTML (Internet Explorer) or CSV (Text file).
  • Auto Arrange – enable/disable automatic sizing of fields. The same action is performed by pressing A.
  • Grid – show/hide grid to separate fields. The same action is performed by pressing G.