question for #define experts - page 4

 
Alexandr Andreev:

Deployment of normal functions is a matter of course

i.e. for example for (int i=0; i<ArraiSize(max); i++)

here ArraiSize(max); will be expanded and it will get something like address to size of given array (if we look at array, it has its size in a variable, and here we have substitution on this variable "address in memory") i.e. there is no sense to change it to a variable, at all

for (int i=0; i<ArraiSize(max); i++)

и

for (int i=0; i<size; i++ )

In this case ArraiSize(max) and size have the same timings to determine the array size

incorrect statement

tested: I tried it 1000 times, 3 arrays and 3 nested loops in each array, 2 variants: the 1st and the 2nd

result is stable (tested 3 times):

2020.11.02 21:17:25.952 SpeedTst (EURUSD,H1) ArraySize: loops = 1000 seconds=114.5013

2020.11.02 21:17:40.713 SpeedTst (EURUSD,H1) cnt: loops = 1000 seconds=99.7722

I don't see the point in discussing testing methodology - very long and necessary
 
Roman:

In this example loop, I don't think I agree about the timings.
On the contrary, it is recommended to get the result in size variable and use it in the condition.
Since the loop at each iteration forArraiSize(max) will unwind unnecessarily, slowing down loop execution.

The ArraiSize(max) function expands and returns the cell with the length record from the array;the address of the cell where the length of the array is stored is returned

and what is any variable, in this case size - is the addressof the cell where the length of the array is stored

So, we get the same thing at the output and even if we change the size of the array in the process of the loop, the address per cell won't change either in the first or in the second case.

Since the mas value is always static (it's not a reference), there simply can't be any other logic)))

The ArraiSize(max) function itself , says that a memory section in themax array should be used for the array length; this is performed at the compiling stage - function deployment

 
Igor Makanu:

incorrect statement

tested: metering 1000 times, 3 arrays and 3 nested brute force loops on each array, 2 variants: 1st and 2nd

result is stable (tested 3 times):

2020.11.02 21:17:25.952 SpeedTst (EURUSD,H1) ArraySize: loops = 1000 seconds=114.5013

2020.11.02 21:17:40.713 SpeedTst (EURUSD,H1) cnt: loops = 1000 seconds=99.7722

I don't see the point in discussing testing methodology - very long and necessary

then some problems with the build....

 
Alexandr Andreev:

then there are some problems with the assembly....

It's clear that the output is the same.
But this memory section will be accessed differently, that was the point.
Access to a variable is faster than to a function, since the variable already contains the value.
And a function still needs to retrieve this value and return it, i.e. refer to a memory cell, an unnecessary instruction.

Igor's example is just a confirmation of what I was saying.

 
Alexandr Andreev:

then some problems with the assembly....

lied

ArraySize() is called at each iteration

even if you don't check the array size each time, you still get a procedure call and at least a check of the variable storing the array size

this is the test I'm writing about, we resize the array in the loop, the loop is interrupted as it should be, i.e. at each iteration of the loop the array size is determined after the loop body is executed:

void OnStart()
{
   int arr[];
   int sz = ArrayResize(arr, 100000);
   int cnt = 0;
   for(int i = 0; i < ArraySize(arr); i++)
   {
      ArrayResize(arr, sz--);
      cnt++;
   }
   printf("cnt = %i", cnt);   //cnt = 50001
}
 
Igor Makanu:

lied

ArraySize() is called at each iteration

even if the array size is not checked every time, the procedure is still called and at least the variable storing the array size is checked

Here's the test I'm writing about, changing the size of the array in the loop, the loop was terminated as expected, i.e. at each loop iteration the size of the array was defined after the loop body was executed:

Give me the full code of your test.

Roman:

It's clear that we get the same thing in the output.
But this memory fragment will be fetched differently, that's what I mean.

Igor's example is just a confirmation of what I was saying.

And you yourself conducted the test, I'm at 110000000000000000000 something like that doesn't happen.

More precisely, I don't have it at all in any tests, even atLONG_MAX repetitions
 
Alexandr Andreev:

ArraiSize(max) function is expanded, it takes a cell with the length record from the array and returns it,the address of the cell where the length of the array is recorded

and what is any variable, in this case size - is the addressof the cell where the length of the array is stored

So, we get the same thing at the output and even if we change the size of the array in the process of the loop, the address per cell won't change either in the first or in the second case.

Since the mas value is always static (it's not a reference), there simply can't be any other logic)))

The ArraiSize(max) function itself says that we should use a memory area in themax arrayresponsible for the array length and this is executed at the compilation stage - function deployment

This opinion seems to be premature. What exactly happens behind the ArraiSize function is unknown, isn't it? It's a black box in MQL. It's quite possible that for (int i=0; i<ArraiSize(max); i++) will lead to the execution of several instructions. For example, a function may check the input parameters. Save - push the data off the stack. Save registers. Restore registers. Function call - call, then return. What I mean is that a lot may be happening in the background and we cannot see it. That's why it's better to use for (int i=0; i<size; i++) and not rely on the compiler to do what we expect it to do.

 
Alexandr Andreev:

Did you do the test yourself, I don't have it at 110000000000000000000


It was tested a long time ago, that's why I only use variables in loops.

 
Roman:

This was tested a long time ago, so I only use variables in loops.

Show me a test with the code

 
Alexandr Andreev:

Show me the code test

What test? ))
You yourself showed both variants of the loop condition.
Igor gave the code above too.
Just measure the loop's execution with the size variable and with ArraySize() in the loop condition.

Reason: