fxsaber #: Why does the second variant work 20 times slower?
The memcpy(Array, Array, 0) trick to get the address is a clever hack, but it is antipattern to use it for element-by-element access to an array. DLL-calls make sense only for batch operations where one call processes a large block of data.
If you need low-level memory access in MQL5, it is better to write all the logic inside one DLL-function rather than pulling it millions of times from MQL.
The reason is obvious: overhead of calling the DLL-function at each iteration.
This confirms that the MQL5 compiler is smarter than it seems. Apparently, the compiler sees 8 different indexes, 8 separate boundary checks at each iteration, and as a result it loses autovectorisation (does not recognise the pattern). 0.3 ns per iteration - I think this is almost the limit. Even C++ DLL will not give a big gain. We are practically stuck in memory bandwidth, not CPU bandwidth. Further only to change the architecture of the solution. Perhaps using GPU (OpenCL) will give some results, but this is only in case there are multiple operations on one buffer or more complex calculations than just sum. For a single sum - GPU can be comparable or even slower because of transfer overhead.
Allow the use of cookies to log in to the MQL5.com website.
Please enable the necessary setting in your browser, otherwise you will not be able to log in.
You are signed in. Two-factor authentication has been disabled. Set it up again and scan a new QR code, or continue with two-factor authentication disabled.
The preprocessor knows nothing about the value of 'o' or any variable.
What are you REALLY trying to achieve ?
This way you can use the compilation conditions
Works just fine. Thank you.
Why is the second option 20 times slower?
1. calling an external DLL.
2. copying from memory.
Why does the second variant work 20 times slower?
@amrali, @Nikolai Semko, thank you!
I wanted to implement direct access to the array without checking if the index is correct.
@amrali, @Nikolai Semko, thank you!
I wanted to implement direct access to the array without checking if the index is correct.
This variant is three times slower.
This option is three times slower.
Apparently, the compiler sees 8 different indexes, 8 separate boundary checks at each iteration, and as a result it loses autovectorisation (does not recognise the pattern).
0.3 ns per iteration - I think this is almost the limit. Even C++ DLL will not give a big gain. We are practically stuck in memory bandwidth, not CPU bandwidth.
Further only to change the architecture of the solution. Perhaps using GPU (OpenCL) will give some results, but this is only in case there are multiple operations on one buffer or more complex calculations than just sum. For a single sum - GPU can be comparable or even slower because of transfer overhead.