Questions on OOP in MQL5 - page 71

 
Igor Makanu:

if you write such classes:

judging from your research, class B will be slower to execute if you use structure fields frequently in calculations?

So, we were discussing the time of traversing an array of objects)))

According to your example it will be the same in pluses, but in mql there's a nuance, there are two implicit fields which go first, so, data field will be approached by shift, i.e. additional calculations during dereferencing.

 
Vladimir Simakov:

So, we were discussing the time of traversing the array of objects)))

According to your example it will be the same in pluses, but in mql there's a nuance, there are two implicit fields, which go first, so, data field will be approached by shift, i.e. additional calculations during dereferencing.

Thank you, that's helpful!

 
Vladimir Simakov:

So, we were discussing the object array traversal time)))

According to your example it will be the same in pluses, but in mql there's a nuance: there are two implicit fields which go first, so data field will be accessed at an offset, i.e. additional dereferencing calculations will be performed.

Both classes are similar. Access is by offset in both cases, only not relative to the beginning of the structure, but relative to the beginning of the class. In other words, the structure itself is weightless. Only classes create an overhead.
 
Vladimir Simakov:

So, no mysticism - the laws of physics in action.

It doesn't fit the "laws of physics".

#include <fxsaber\Benchmark.mqh> // https://c.mql5.com/3/321/Benchmark.mqh

long f1( const int Size, const long Amount = 5 e9 )
{
  long Sum = 0;
  
  for (long i = 0; i < Amount; i++)
  {
    Sum += Size * i;
    
    Sum |= 1;
  }
    
  return(Sum);
}

long f2( const int Size, const long Amount = 5 e9 )
{
  long Sum = 0;
  
  for (long i = 0; i < Amount; i++)
  {
    Sum += Size * (Amount - i);
    
    Sum |= 1;
  }
    
  return(Sum);
}

void OnStart()
{
  Print(_B(f1(2), 1));
  Print(_B(f1(200), 1));
  
  Print(_B(f2(2), 1));
  Print(_B(f2(200), 1));
}


        Alert: Time[Test6.mq5 267: f1(2)] = 3252 ms.
        6553255921290448385
        Alert: Time[Test6.mq5 268: f1(200)] = 4602 ms.
        -8757194524499019775
        Alert: Time[Test6.mq5 270: f2(2)] = 3061 ms.
        6553255931290448385
        Alert: Time[Test6.mq5 271: f2(200)] = 3112 ms.
        -8757193524499019775


This is a paradoxical result. More complex calculations are performed 1.5 times faster and do not depend on size.

 
Vladimir Simakov:

So, we were discussing the object array traversal time)))

According to your example it will be the same in pluses, but there's a subtlety in mql: there are two implicit fields that go first, so data field will be accessed at an offset, i.e. additional dereferencing calculations will be performed.

Thanks to Vladimir for the assembler study.
And as Alexey suggests, the overhead is created by classes.
From this we can conclude that if you can do without classes, it's better to write code in procedural style.
That is, if the task does not require speed, you can wrap it in a class, but if you are dealing with ticks, for example, you'd better use it directly without wrappers.
In principle, this is the approach I followed, and often finding an example of a class, I disassemble its methods in procedural approach.

 
Roman:

Thanks Vladimir for the assembly study.
And as Alexey suggests, overhead creates classes.
From this we can conclude that if we can do without classes, it is better to write code in procedural style.
That is, if the task does not require speed, you can wrap it in a class, but if you are dealing with ticks, for example, you'd better write directly without wrappers.
In principle, this is the approach I've been following and often upon finding an example of a class, I disassemble its methods using procedural approach.

There's a troll on the forum...
I used to not understand why some users ask for the ignore function, ah how it's missing now...

 

Forum on trading, automated trading systems and strategy testing

OOP questions in MQL5

fxsaber, 2020.05.30 14:06

I have no idea what to do with it. I have made simple structures.

        50000000
        50000000
        Alert: Time[Test6.mq5 280: Func(Array1)] = 312 ms.
        1333106752
        Alert: Time[Test6.mq5 281: Func(Array3)] = 1348 ms.
        1333106752
        : sizeof(STRUCT1) = 20
        : sizeof(STRUCT3) = 84

I'm not sure why the access to the first field of a simple structure depends on its size.

You are using 50M elements in an array.
For structures of size 20 and 84 bytes this is 0.93GB and 3.91GB of data, respectively.
And in the frame of your calculation, presumably, all this memory goes through the processor's cache.
And a very logical explanation for these results would be that 0.93 GB of data would be downloaded from memory into the CPU cache four times faster than the 3.91 GB of data.

What about the results of the C++ test?
Because I've seen assembler code, but no test results, or did I look badly?
 
Sergey Dzyublik:

There's a troll on the forum...
I used to not understand why some users ask for the ignore function, ah how it's missing now...

You should watch out for yourself, not others.
Not you and not for you was the answer.
Ignore in silence ))

 
Sergey Dzyublik:
You are using 50M elements in an array.
For structures of 20 and 84 bytes, that's 0.93GB and 3.91GB of data respectively.
And as part of your calculation, presumably all of this memory goes through the processor's cache.
And a very logical explanation for these results would be that 0.93 GB of data would be downloaded from memory into the CPU cache four times faster than the 3.91 GB of data.

What about the results of the C++ test?
Because I've seen assembler code, but no test results, or did I look badly?
It works in the same way. It's only a bit faster, but the ratio is the same, so it's hardware.
 
Roman:

Thanks Vladimir for the assembly study.
And as Alexey suggests, overhead creates classes.
From this we can conclude that if we can do without classes, it is better to write code in procedural style.
That is, if the task does not require speed, you can wrap it in a class, but if you are dealing with ticks, for example, you'd better use it directly without wrappers.
In principle, this is the approach I take, and often finding an example of a class, I disassemble its methods in procedural style.

You can use a structure instead of a class, they're fine.