Features of the mql5 language, subtleties and tricks - page 308
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
b5179, I tried to speed up calculations by creating a table of pre-calculated data. But I didn't get any acceleration, because every access to an array element by index inside MQL5 causes checking of index correctness, even if everything in the code is error-free.
Then I decided to try to create an object that has FASTER access to the index than the array. And I "succeeded"!
I put it in quotes because I got unexplained results.
The performance of this code depends greatly on whether optimisation is selected in the compiler or not. And also on the included instructions (x64, AVX, ...).
It turns out that there is a way to access items by index 8-9% faster than array items.
And then inexplicable things begin, which I highlighted in the source code. For some reason, the table is calculated so quickly only if it is wrapped in a macro. Because of this macro I had to add a corresponding #include. And I can't reproduce this macro without this #include - it slows down hundreds of times.
The same wild slowdown also occurs when the number of calculated elements increases a bit - see the comment to AMOUNT.
Another complaint about the compiler. In the source code the index of an array element is uchar-type, and the number of elements of the corresponding static array is 2048 - which is obviously more than UCHAR_MAX. So why does the compiler checks each time the index for going outside the array?
Please do not make such checks in such situations. And please explain the anomalous behaviour of the script above. Like and inform if it is possible to make data tables in static arrays so that there are no checks on the index every time it is accessed?
Search string: Uluchshenie 130.b5179, I tried to speed up calculations by creating a table of pre-calculated data. But I didn't get any acceleration, because every access to an array element by index inside MQL5 causes checking of index correctness, even if everything in the code is error-free.
5. It would be more interesting if you measure random access times instead of sequentially to negate the effect of cpu cache on results:
There are a few things to consider here:
1. Index checking for arrays is only done at compile time (if the compiler can detect array boundaries) and never at runtime, so you may get an "array out of index" error at runtime if your index is outside the array boundaries.
I disagree. It is at runtime that there is a check for array out of bounds (as well as a check for pointer validity every time you work through it).
Forum on trading, automated trading systems and testing trading strategies.
New version of MetaTrader 5 build 4260: general improvements
Renat Fatkhullin, 2024.03.28 02:01
Language security requirements do not allow to skip index access control.
I disagree. It is at runtime that there is a check for array out of bounds (as well as a check for pointer validity every time you work through it).
A good way to mix up the indexes. Thanks.
Simpler mix (Fixed):
If you do an additional bit operation during addition, the execution speed increases by 40%.
Search string: Oshibka 139.
b5179, another interesting compiler behaviour.
If you do an additional bit operation during addition, the execution speed increases by 40%.
Search string: Oshibka 139.
By the way, I accidentally discovered that ArrayInitialise(array, 0) is much faster than ZeroMemory(array) for long[] arrays.
The second function zeroes the array size.
The second function zeroes the array size.