Coding to save resources

 

Hi,

My EA has the potential of having a large number of classes and I don’t want to use their functionality if they have not been selected from the user inputs. So to reduce used resources I thought of instantiating a class only if needed.

So, my question is, do you think that the following coding will use less resources?

CClass1 *Class1 
CClass2 *Class2  
CClass3 *Class3 
CClass4 *Class4   

OnInit() 
if(selected) Class1 = new CCLass1; 
if(selected) Class2 = new CCLass2; 
if(selected) Class3 = new CCLass3 
if(selected) Class4 = new CCLass4; 

Than this

CClass1 Class1 
CClass2 Class2 
CClass3 Class3 
CClass4 Class4 
Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2024.09.11
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 

If those objects do not contain large arrays, then they take up very little RAM and there is no point in bothering with it. In In this case, write in a way that is convenient for you.

 

You can create an instance of a class in a script and deliberately not delete it to find out how much memory it takes up (as a lazy way).

#property strict

class Foo
  {
   long   a;
   long   b;
   long   c;
   double d[50];
  };

void OnStart()
  {
   Foo* foo = new Foo();
  }


 

As for performance, accessing an object through a dynamic pointer will always be a little slower, since the terminal will check the validity of the pointer every time the object is accessed.

From this point of view, objects with an automatic pointer are a little better.

But the overhead associated with the dynamic pointer can also be neglected, since it still works quite fast (+1-2 nanoseconds for each pointer check by the terminal, if I'm not mistaken).

 
Vladislav Boyko #:

You can create an instance of a class in a script and deliberately not delete it to find out how much memory it takes up (as a lazy way).


Thanks for the advise.