Single array calculation during optimisation - page 6

 
Alexey Viktorov:

Here is an example of writing and reading an array

Run it twice in debug mode and see how long it takes to write and read the array. The first run will write the array to file, and the second will read it and start printing dates from the array. Then figure out the highlighted part, if IS_DEBUG_MODE should be removed, and if IsOptimization() should be inserted in the condition.

Thanks!
 
Dmitry Fedoseev:

There is a function, something like FileWriteInteger(). Identify by location, the variable must be at the beginning of the file, after reading it the pointer will move to its end, i.e. to the beginning of the array, after reading the array the pointer will be before the next variable...

An article on working with files will be published soon.

I wanted to write you that there is no article on file operations. You make them informative and lucid. I'll certainly read them when they appear.

 

Tried to declare an array at global level (mql4 code)

datetime T[];

fill it in the inite

ArrayResize(T,40);
datetime T[40]=
   {
   1262731020,1262735700,1262821920,1262903400,1262989740,1263247200,1263339060,1263420000,1263507480,1263595500,
   1265324700,1265407200,1265752980,1265926500,1265930820,1267657200,1267740300,1267826460,1268175840,1268346360,
   1270504920,1270684140,1270768500,1272924180,1273011720,1273097100,1273272240,1273528800,1273617180,1275344100,
   1275516000,1275602400,1275689160,1276034400,1276124580,1276208700,1276211640,1278027960,1278369780,1278373920
   };

When unset at start, it prints date 1971, i.e. array is empty

for (int x=0;x<=39;x++)
   {
   Print("T[x]=",T[x],"x=",x);   
   }

If I fill the array in the start, the values appear. May be the reason, that the array is re-initialized datetaime, but without it the code won't compile?

The array disappears. What I do wrong?

 
forexman77:

Tried to declare an array at global level (mql4 code)

fill it in the inite

When unset at start, it prints date 1971, i.e. array is empty

If I fill the array in the start, the values appear. May be the reason, that the array is re-initialized datetaime, but without it the code won't compile?

The array disappears. What I do wrong?

Look what the compiler says.

ArrayResize(T,40);
datetime T[40]=

With such an easy move, you have defined a local array T that will vanish on exit of OnInit...

 
Maxim Kuznetsov:

check the warning that the compiler generates.

With such an easy move, you have defined a local array T that will be lost when you exit OnInit...

It says there is already a global declaration)

declaration of 'T' hides global declaration at line 40

But, how to do it right. I had never declared an array using curly brackets before. I initialized it with zero and set its size and then filled it with the loop. Is there some other way to do it?

If I clear the datetime in the inite, the code does not compile, but writes two errors:

'{' - expression expected

'=' - illegal operation use



 
forexman77:

It says there is already a global declaration)

declaration of 'T' hides global declaration at line 40

But, how to do it right. I had never declared an array using curly brackets before. I initialized it with zero and set its size and then filled it with the loop. Is there some other way to do it?

If I clear the datetime in the inite, the code does not compile, but writes two errors:

'{' - expression expected

'=' - illegal operation use



The array declaration together with constant initialization could (and should) be done at the global level.

datetime T[40]=
   {
   1262731020,1262735700,1262821920,1262903400,1262989740,1263247200,1263339060,1263420000,1263507480,1263595500,
   1265324700,1265407200,1265752980,1265926500,1265930820,1267657200,1267740300,1267826460,1268175840,1268346360,
   1270504920,1270684140,1270768500,1272924180,1273011720,1273097100,1273272240,1273528800,1273617180,1275344100,
   1275516000,1275602400,1275689160,1276034400,1276124580,1276208700,1276211640,1278027960,1278369780,1278373920
   };
void OnInit() 
{
 ....
}
 
forexman77:

It says there is already a global declaration)

declaration of 'T' hides global declaration at line 40

But, how to do it right. I had never declared an array using curly brackets before. I initialized it with zero and set its size and then filled it with the loop. Is there some other way to do it?

If I clear the datetime in the inite, the code does not compile, but writes two errors:

'{' - expression expected

'=' - illegal operation use



With '=' you may initialize arrays only when declaring them. It seems to be a remnant of C, but that's how it is done here :-)
 
Maxim Kuznetsov:

The array declaration along with constant initialization could (and should) have been done at the global level.

Yes, I tried it and it works. And you can't do it without global level, I just did it to reduce the computational cost, to fill the array once in the init and then use the array values?

Although logically, the variables on the global level are always constant, so the calculation is performed once at the beginning of the program?

 
forexman77:

Yes, I tried it and it works. And you can't do it without global level, I just did it to reduce the computational cost, to fill the array once in the init and then use the array values?

Although logically the variables on the global level are always constant, so the calculation is performed once at the beginning of the program?

Yes, when preparing an ex4 ex5 binary for execution by the terminal. You can think that it's just links to the data array described (and saved inside ex4/5 at compile time), and that's very fast...
 
forexman77:

It says there is already a global declaration)

declaration of 'T' hides global declaration at line 40

But, how to do it right. I had never declared an array using curly brackets before. I initialized it with zero and set its size and then filled it with the loop. Is there some other way to do it?

If I clear the datetime in the inite, the code does not compile, but writes two errors:

'{' - expression expected

'=' - illegal operation use



It doesn't really matter how the array is filled. You simply have to set a condition whereby if the array's size is zero, it should be evaluated, filled and written into a file. And in OnInit(), try to read into the declared array using my example. Accordingly, if there is a file array, it is read, and the array is filled, the array will not be recalculated and filled again.
Reason: