Features of the mql5 language, subtleties and tricks - page 117
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
This is the variant I've come up with:
It's supposed to be the fastest of all possible ones. All calculations are performed with constants, so they are calculated during compilation. So, everything is reduced to just 6 consecutive comparisons and nothing more. However, this variant works slower than the previous one. I can't understand the reason for that.
This is the variant I've come up with:
In idea, this is the fastest of all possible. All calculations are made with constants, so they are calculated during compilation. So, everything is reduced to just 6 consecutive comparisons, and nothing more. However, this variant works slower than the previous one. I can't understand what the reason is.
Dividing by two slows it down ? Try to replace with a shift? I suspect that the calculated constants - must be calculated immediately (in this case - and the shift in the define - must also be replaced by a constant).
Besides, "question" is a rather controversial operator, as I know. Twenty years ago it was checked in C++ and sometimes "question" generates much longer code than the usual if operator. Maybe it's the same here ?
And, I would make the return code uint - what if there are some checks when converting signed and unsigned values?
I haven't got an opportunity to experiment manually yet - the CPU is way overloaded... Even text is typed "with slowness"...
Dividing by two slows it down ? Try to replace it with a shift ? I suspect that the calculated constants - must be calculated immediately (in this case - and the shift in the define - must also be replaced by a constant).
Also - "question" - as I know, it's quite a controversial operator ...
Replacing division with shift has no effect. I suspect that the resulting expression is too long, so the compiler didn't optimize it to the end.
But I ran the tests when Optimize=0, while when optimization was enabled, everything went well - the second variant was one and a half times faster. Bingo!
If optimisation is disabled, then the second option is slightly slower at small values, but slightly faster at larger ones. In short, the second option is definitely better.
This is the variant I've come up with:
This is supposedly the fastest of all possible variants. All calculations are performed with constants, so they are calculated during compilation. So, everything is reduced to just 6 consecutive comparisons and nothing more. However, this variant works slower than the previous one. I cannot understand the reason for that.
That's right - your variant is the fastest.
It's just that the test is idle. Programmers very often forget one important thing when testing for performance: if a calculated value is not used anywhere, the compiler simply won't perform the computation.
It makes sense, what's the point? It's like in quantum superposition. Why should the moon exist if no one is looking at it. "Does the moon exist just because a mouse is looking at it?" (Albert Einstein). :))
So this version of the test with the checksum calculation and printing it would be more correct:
Result:
And second place is still _FastLog2, not log2 :))It's just an idle test. An important point is often forgotten in performance tests: if the calculated value is not used anywhere, the compiler simply doesn't perform the computation.
It makes sense, what's the point? It's like in quantum superposition. Why should the moon exist if no one is looking at it. "Does the moon exist just because a mouse is looking at it?" (Albert Einstein). :))
So this version of the test with the checksum calculation and printing it will be more correct:
Your code is tangled. The variables used in the define are located at the other end of the program code - it's not convenient to sort through such chaos. But that's not the point. The point is that the results of your tests cannot be considered reliable because the compiler knows in advance the algorithm of values passed into the function. So it optimizes your tests. You should compute on random numbers .
By the way, why do you have srand in your code? When I saw it, at first I thought you were using random, but actually you're not.
Here is my code:
Your code is confusing. The variables used in the define are located at the other end of the program code - it is inconvenient to sort through such chaos. But this is not the point, the point is that your test results cannot be considered reliable, because the compiler knows in advance the algorithm of values passed into the function. Therefore it optimizes your tests. You should calculate on random numbers .
By the way, why do you have srand in your code? When I saw it, at first I thought you were using random, but actually you're not.
Here is my code:
the code is not mine. I just tweaked it and removed rand to check the same checksums and remove relatively expensive rand function from the loop, but I simply forgot to remove srand.
I return rand. You're right - the compiler does optimize the loop for the sum of logarithms from consecutive values. I'm surprised, though. I don't understand how it does that. Perhaps there's something we're not taking into account.
Result:
The current winner is _FastLog2
Result:
Current winner _FastLog2
I wonder how you got the same checksum everywhere, if the values are random.
I wonder how you get the same checksum everywhere, if the values are random.
srand(45) for all functions
I just did it that way at first, but got different checksums, because I didn't take into account that rand()*rand() can be 0, which breaks the checksum. Now I added one to get away from zero.
srand(45) for all functions
I just did the same at first, but got different checksums, because I didn't take into account that rand()*rand() can be 0, which breaks the checksum. Now I added one to get away from zero.
And why do you need the same checksum if we're talking specifically about speed measurements? The point of the sum in this case is simply to prevent the compiler from cutting the code, that's all. And by doing srand(45), you again allow to optimize the test.
By the way, speaking of zero, FastLog2 doesn't check for zero, which gives it a head start. But it's still one and a half to two times slower than log2 if tested correctly).
And why do you need the same checksum if we are speaking specifically about speed measurements? The point of the sum in this case is simply to prevent the compiler from cutting the code, that's all. And doing srand(45), again, allows you to optimize the test.
You are overestimating the capabilities of the compiler here. Remove srand(45) - checksums will be different, but the speed result remains the same.
Moreover, I was guided by the fact that the calculations were the same for the sake of purity of the experiment, because I didn't go into details of all the functions. Sometimes the value of a function parameter can affect the time of its execution.
All the more reason to check the correctness of the algorithms.