Questions on OOP in MQL5 - page 87

 
Vladimir Simakov:
Question. Why do you need an accordion? If in one method, one class, in spherical horse, yes, in physical vacuum (well, I'm drunk)))), then yes, it makes sense. And if in a real project, either a class at the global level, or a loner that handles all this trichomudin (sorry for the extra bourbon))))) at the beginning of the handler, and, request directly from it at any place)))

it's a class manager who distributes tasks and gets results from a small herd of subordinate classes ))))

in general, it is difficult to explain in a sober way

but the question is relevant - how addressing of the common fields is performed when reading the values of these fields from different methods and very often

 
Igor Makanu:

it's a class manager who distributes tasks and gets results from a small herd of subordinate classes ))))

in general, it is difficult to explain in a sober way

but the question is relevant - how addressing of the common fields is performed when reading the values of these fields from different methods and very often

Look. At the beginning of the handler, no matter which, you start gSomeGlobal.SomeEvent(...), or, if single, Alone::SomeEvent(...), in this method do the required state handling and, then, simply request the required data via getters. As long as you're not a high-frequency scalper, you can screw all speed advocates, although there's no special overhead here)))
 
Vladimir Simakov:
See also. At the beginning of the handler, no matter which, you start gSomeGlobal.SomeEvent(...), or, if single, Alone::SomeEvent(...), in this method you do the required state processing and, then, you just request the required data via getters. As long as you're not a high-frequency scalper, you can screw all speed advocates, although there's no special overhead here)))

mt5 performance is enough for 10500 orders per second, the channel would have been enough

I was looking for the truth, I had to check it... if you want to do something, do it yourself! )))

#property copyright "IgorM"
#property link      "https://www.mql5.com/ru/users/igorm"
#property version   "1.00"

#define  TST_COUNT 1 e10

#define    SpeedTest(count,msg,EX)   {uint mss=GetTickCount(); ulong lim = count; for(ulong cnt=0;cnt<lim&&!_StopFlag;cnt++){EX;} \
                                    printf("%s: loops = %llu ms=%u",msg,lim,GetTickCount()-mss);}
//+------------------------------------------------------------------+
class A
{
private:
   double            _Ask, _Bid;
   double            f1()  { return(_Ask + 1.0/(1.0+(double)rand())); }
   double            f2()  { return(_Bid + 1.0/(1.0+(double)rand())); }
   double            f3()  { return(_Ask/_Bid + 1.0/(1.0+(double)rand())); }
public:
   double            calc(const MqlTick &tick){ _Ask = tick.ask; _Bid = tick.bid; return(f1() + f2() + f3()); }
};
//+------------------------------------------------------------------+
class B
{
private:
   double            f1(const MqlTick &t)  { return(t.ask + 1.0/(1.0+(double)rand())); }
   double            f2(const MqlTick &t)  { return(t.bid + 1.0/(1.0+(double)rand())); }
   double            f3(const MqlTick &t)  { return(t.ask/t.bid + 1.0/(1.0+(double)rand())); }
public:
   double            calc(const MqlTick &tick){ return(f1(tick) + f2(tick) + f3(tick)); }
};
//+------------------------------------------------------------------+
void OnStart()
{
   A a;
   B b;
   MqlTick rnd_tick;
   for(int i=0; i<5; i++)
   {
      SpeedTest(TST_COUNT, "class A : ",
               rnd_tick.ask = 1.0 + 1.0 / (1.0 + (double)rand());
               rnd_tick.bid = 1.0 + 1.0 / (1.0 + (double)rand());
               double res = a.calc(rnd_tick);
      );
   
      SpeedTest(TST_COUNT, "class B : ",
               rnd_tick.ask = 1.0 + 1.0 / (1.0 + (double)rand());
               rnd_tick.bid = 1.0 + 1.0 / (1.0 + (double)rand());
               double res = b.calc(rnd_tick);
      );
   }
}
//+------------------------------------------------------------------+

2020.07.25 18:00:07.293 class_global (EURUSD,H1) class A : : loops = 10000000000 ms=46141

2020.07.25 18:00:45.273 class_global (EURUSD,H1) class B : : loops = 10000000000 ms=37968

2020.07.25 18:01:31.405 class_global (EURUSD,H1) class A : : loops = 10000000000 ms=46141

2020.07.25 18:02:09.423 class_global (EURUSD,H1) class B : : loops = 10000000000 ms=38016

2020.07.25 18:02:55.558 class_global (EURUSD,H1) class A : : loops = 10000000000 ms=46125

2020.07.25 18:03:33.635 class_global (EURUSD,H1) class B : : loops = 10000000000 ms=38078

2020.07.25 18:04:21.969 class_global (EURUSD,H1) class A : : loops = 10000000000 ms=48344

2020.07.25 18:05:00.113 class_global (EURUSD,H1) class B : : loops = 10000000000 ms=38140

2020.07.25 18:05:46.503 class_global (EURUSD,H1) class A : : loops = 10000000000 ms=46391

2020.07.25 18:06:24.573 class_global (EURUSD,H1) class B : : loops = 10000000000 ms=38062

The speed is critical for optimization, that's why I wanted to know about it.

in general, the reference must pass the current price to all methods



UPD:

//+------------------------------------------------------------------+
class C
{
private:
   double            f1(const double &a, const double &b)  { return(a + 1.0/(1.0+(double)rand())); }
   double            f2(const double &a, const double &b)  { return(b + 1.0/(1.0+(double)rand())); }
   double            f3(const double &a, const double &b)  { return(a/b + 1.0/(1.0+(double)rand())); }
public:
   double            calc(const MqlTick &tick){const double ask = tick.ask; const double bid = tick.bid; return(f1(ask,bid) + f2(ask,bid) + f3(ask,bid)); }
};
//+------------------------------------------------------------------+

2020.07.25 19:03:37.210 class_global (EURUSD,H1) class A : : loops = 10000000000 ms=46141

2020.07.25 19:04:15.201 class_global (EURUSD,H1) class B : : loops = 10000000000 ms=38000

2020.07.25 19:04:53.188 class_global (EURUSD,H1) class C : : loops = 10000000000 ms=37984

2020.07.25 19:05:39.321 class_global (EURUSD,H1) class A : : loops = 10000000000 ms=46125

2020.07.25 19:06:17.313 class_global (EURUSD,H1) class B : : loops = 10000000000 ms=38000

2020.07.25 19:06:55.306 class_global (EURUSD,H1) class C : : loops = 10000000000 ms=37985

 
Igor Makanu:

mt5 performance is enough for 10500 orders per second, the channel would have been enough

I was looking for the truth, I had to check it... if you want to do something, do it yourself! )))

2020.07.25 18:00:07.293 class_global (EURUSD,H1) class A : : loops = 10000000000 ms=46141

2020.07.25 18:00:45.273 class_global (EURUSD,H1) class B : : loops = 10000000000 ms=37968

2020.07.25 18:01:31.405 class_global (EURUSD,H1) class A : : loops = 10000000000 ms=46141

2020.07.25 18:02:09.423 class_global (EURUSD,H1) class B : : loops = 10000000000 ms=38016

2020.07.25 18:02:55.558 class_global (EURUSD,H1) class A : : loops = 10000000000 ms=46125

2020.07.25 18:03:33.635 class_global (EURUSD,H1) class B : : loops = 10000000000 ms=38078

2020.07.25 18:04:21.969 class_global (EURUSD,H1) class A : : loops = 10000000000 ms=48344

2020.07.25 18:05:00.113 class_global (EURUSD,H1) class B : : loops = 10000000000 ms=38140

2020.07.25 18:05:46.503 class_global (EURUSD,H1) class A : : loops = 10000000000 ms=46391

2020.07.25 18:06:24.573 class_global (EURUSD,H1) class B : : loops = 10000000000 ms=38062

The speed is critical for optimization, that's why I wanted to know about it.

in general, the reference must pass the current price to all methods



UPD:

2020.07.25 19:03:37.210 class_global (EURUSD,H1) class A : : loops = 10000000000 ms=46141

2020.07.25 19:04:15.201 class_global (EURUSD,H1) class B : : loops = 10000000000 ms=38000

2020.07.25 19:04:53.188 class_global (EURUSD,H1) class C : : loops = 10000000000 ms=37984

2020.07.25 19:05:39.321 class_global (EURUSD,H1) class A : : loops = 10000000000 ms=46125

2020.07.25 19:06:17.313 class_global (EURUSD,H1) class B : : loops = 10000000000 ms=38000

2020.07.25 19:06:55.306 class_global (EURUSD,H1) class C : : loops = 10000000000 ms=37985

For starters make the same working conditions)))) Because you have inserted an absolutely unnecessary assignment operator (2 pcs), thus creating a knowingly non-optimal code)))

//+------------------------------------------------------------------+
class A
{
private:
   double            _Ask, _Bid;
   double            f1()  { return(_Ask + 1.0/(1.0+(double)rand())); }
   double            f2()  { return(_Bid + 1.0/(1.0+(double)rand())); }
   double            f3()  { return(_Ask/_Bid + 1.0/(1.0+(double)rand())); }
public:
   void              Init() {_Ask = 1.0 + 1.0 / (1.0 + (double)rand());
                             _Bid = 1.0 + 1.0 / (1.0 + (double)rand());}
   double            calc(){return(f1() + f2() + f3()); }
};
//+------------------------------------------------------------------+
class B
{
private:
   double            f1(const MqlTick &t)  { return(t.ask + 1.0/(1.0+(double)rand())); }
   double            f2(const MqlTick &t)  { return(t.bid + 1.0/(1.0+(double)rand())); }
   double            f3(const MqlTick &t)  { return(t.ask/t.bid + 1.0/(1.0+(double)rand())); }
public:
   double            calc(const MqlTick &tick){ return(f1(tick) + f2(tick) + f3(tick)); }
};
//+------------------------------------------------------------------+
void OnStart()
{
   A a;
   B b;
   MqlTick rnd_tick;
   for(int i=0; i<5; i++)
   {
      SpeedTest(TST_COUNT, "class A : ",
               a.Init();
               double res = a.calc();
      );
   
      SpeedTest(TST_COUNT, "class B : ",
               rnd_tick.ask = 1.0 + 1.0 / (1.0 + (double)rand());
               rnd_tick.bid = 1.0 + 1.0 / (1.0 + (double)rand());
               double res = b.calc(rnd_tick);
      );
   }
}

And now a question: what is more convenient to you? Of course, you can drag a chain of links through method parameters, or you can do it nicely. Compare the speeds yourself)))

 
Vladimir Simakov:

But you took an absolutely unnecessary assignment operator (2 pcs.), and thus created a knowingly sub-optimal code)))

That wasn't me!

It's IBM Knowledge Center !https://www.ibm.com/support/knowledgecenter/ru/ssw_aix_72/performance/coding_style_best_perf.html



The conditions are the same there, every tick you need to call the class manager (or super-class, I don't know)

and it calls the rest of the classes which work according to their strategies

but for the sake of argument, variant C really wins in speed, so it's the knoelege_center that kind of speaks the truth

 
Pavel Verveyko:

I wonder if this is a glitch or a feature)

There is a structure inside the class instance.

I put a dot to see the contents of the structure.
But it is only displayed if I put square brackets.
Although the structure is in a single instance.

the problem is solved if the class is not an array element.




here is the code for "poking around".

try to compile it, even with errors. Sometimes it "wakes up" the editor

 
Igor Makanu:

that's not me!

it's IBM Knowledge Center !the current price to all methods



UPD:

2020.07.25 19:03:37.210 class_global (EURUSD,H1) class A : : loops = 10000000000 ms=46141

2020.07.25 19:04:15.201 class_global (EURUSD,H1) class B : : loops = 10000000000 ms=38000

2020.07.25 19:04:53.188 class_global (EURUSD,H1) class C : : loops = 10000000000 ms=37984

2020.07.25 19:05:39.321 class_global (EURUSD,H1) class A : : loops = 10000000000 ms=46125

2020.07.25 19:06:17.313 class_global (EURUSD,H1) class B : : loops = 10000000000 ms=38000

2020.07.25 19:06:55.306 class_global (EURUSD,H1) class C : : loops = 10000000000 ms=37985

Igor Makanu:

this is not me !

this is IBM Knowledge Center !https://www.ibm.com/support/knowledgecenter/ru/ssw_aix_72/performance/coding_style_best_perf.html



The conditions are the same there, every tick you need to call the class manager (or superclass, I don't know)

and it calls the rest of the classes which work according to their strategies

but for the point - option C really wins in speed, i.e. it's knoelege_center kinda speaks the truth

Of course it wins.) But not because it's written optimally, but because compilers have become clever.) Pure optimization. There is no assignment of Ask and Bid values in the release code))) But when you have a little less parameters to process, and the business logic of the project will be a little more than opening an order by indicator, then I'll see how you get tired of dragging strings of links through the whole project)))) And yes, I already told you about the sin of excessive optimization. Trust me, you don't need it in 98% of cases.

 
Vladimir Simakov:

And yes, I've already told you about the sin of over-optimisation. Trust me, you don't need it 98% of the time.

I do!... but my gut tells me I won't get it until I get it right ))))

Okay, thanks, there's some good sense in this research.

 
Igor Makanu:


UPD:

2020.07.25 19:03:37.210 class_global (EURUSD,H1) class A : : loops = 10000000000 ms=46141

2020.07.25 19:04:15.201 class_global (EURUSD,H1) class B : : loops = 10000000000 ms=38000

2020.07.25 19:04:53.188 class_global (EURUSD,H1) class C : : loops = 10000000000 ms=37984

2020.07.25 19:05:39.321 class_global (EURUSD,H1) class A : : loops = 10000000000 ms=46125

2020.07.25 19:06:17.313 class_global (EURUSD,H1) class B : : loops = 10000000000 ms=38000

2020.07.25 19:06:55.306 class_global (EURUSD,H1) class C : : loops = 10000000000 ms=37985

Try also this way:

class D
{
   double f1(const double &a, const double &b)  { return(a + 1.0/(1.0+(double)rand())); }
   double f2(const double &a, const double &b)  { return(b + 1.0/(1.0+(double)rand())); }
   double f3(const double &a, const double &b)  { return(a/b + 1.0/(1.0+(double)rand())); }
   
public:
   double calc( const MqlTick& tick )
   {
      return f1( tick.ask, tick.bid ) + f2( tick.ask, tick.bid ) + f3( tick.ask, tick.bid );
   }
};
 
Koldun Zloy:

Also try this way:

2020.07.26 09:39:21.350 class_global (EURUSD,H1) class A : : loops = 10000000000 ms=46157

2020.07.26 09:39:59.402 class_global (EURUSD,H1) class B : : loops = 10000000000 ms=38046

2020.07.26 09:40:37.455 class_global (EURUSD,H1) class C : : loops = 10000000000 ms=38063

2020.07.26 09:41:15.485 class_global (EURUSD,H1) class D : : : loops = 10000000000 ms=38031

2020.07.26 09:42:01.749 class_global (EURUSD,H1) class A : : loops = 10000000000 ms=46266

2020.07.26 09:42:39.754 class_global (EURUSD,H1) class B : : loops = 10000000000 ms=38000

2020.07.26 09:43:17.753 class_global (EURUSD,H1) class C : : loops = 10000000000 ms=38000

2020.07.26 09:43:55.743 class_global (EURUSD,H1) class D : : loops = 10000000000 ms=37984

Same values, test speed is "floating" for some reason, but passing parameters in methods by the link is still more efficient