Example of MQL5 code profiling

To add comments, please log in or register
MetaQuotes Software Corp.
Moderator
200090
MetaQuotes Software Corp.  

In build 674 we have introduced the new feature of MQL5 code profiling. Now you can easily assess, locate and optimize performance bottlenecks in your code, having the precise data at hand.

For example, take a look at this calculation heavy script: RayTracer.Bmp.mq5. It makes a large number of calculations and writes the results into a BMP file. Its source code is attached below.

Compile the script and run it on the chart. It works pretty fast and will print its runtime in journal like this:

2012.07.12 19:57:05     RayTracer.Bmp (EURUSD,H1)       In 8393 ms

Generated file Picture.bmp can be found in the \MQL5\Files directory:

The script performs ray tracing to render a complex scene that contains a large number of objects.

Now let's try to estimate the cost of each function and line of this program. To do this, simply run the "Start Profiling" command in MetaEditor, wait while script ends its work on the chart and go back to the editor.

Profiling adds delays to the code control, slowing down the program 3-4 times. This can be observed by the increased generation time:

2012.07.12 20:03:40     RayTracer.Bmp (EURUSD,H1)       In 28127 ms

Take a look at the "Profiler" tab in MetaEditor. The default mode is "Functions by Calls" that shows functions results summary.

This table displays:

  • function name
  • line
  • function calls counter
  • number of cycles spent (measured via rdtsc)
  • graphical representation of usage percentage

Report by functions is intended for general assessment of costs. Click the plus sign icon to expand functions, double-click - to go to the source code.

You can see the detailed cost of resources in the "Functions by Lines" mode that indicates measurements of each line:

The profiler operates in code generation mode with the full optimization cycle. Part of code will not be displayed in its pure form with up to the line precision, because MQL5 compiler makes extensive use of inline functions in order to accelerate programs.

You can export the results of profiling to Open XML, HTML or CSV files.

Well, what can we say? We have made another simple and convenient tool for developers!

Files:
To add comments, please log in or register