Which uses more computer resources?

 

In an mql4 program (and in general) what's 'harder' on the computer: processing and if() statement, initializing a variable, or redefining an existing variable? If you had to put them in order from least taxing to most with special consideration to execution speed, how would you rank them?

Thanks!

 
supertrade:

initializing a variable, or redefining an existing variable?

all variables are always initialized to zero. So it *might* be faster to do

int foo = 42:


instead of

int foo;
foo = 42;


since the latter one would then be equivalent to

int foo = 0;
foo = 42;


But this is only speculation. You should try to benchmark it. I doubt it is worth to even think about this. It would only be relevant in a tight loop that calls a function which declares a local variable. In all other cases the variable already exists. I would then rather inline the contents of the function that is called in the innermost loop.


And then there is the famous saying: "premature optimization is the root of all evil". Write your program and only optimize it after everything is working and only optimize the few hot-spots that are really critical.

 
I wouldn't worry about any of that. The biggest optimization you can make is to filter out all the useless ticks
//+------------------------------------------------------------------+
//| Skip useless ticks                                               |
//+------------------------------------------------------------------+
double  CA.below,   CA.above=0;         // Export to start
void CallAgain(double when){
    if (when>=Bid && CA.above > when) CA.above = when;
    if (when<=Bid && CA.below < when) CA.below = when;
    return;
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start(){
    static datetime Time0;  bool    newBar  = Time0 < Time[0];
    if (Bid > CA.below && Bid < CA.above && (!newBar)) return(0);
    // Don't call me again until the next special case.
    Time0 = Time[0];    CA.below = 0;   CA.above = INF;
...
    static double price=0; Decide(price, reason);           // Calls setDIR
    if (price > 0 && (Bid-price)*DIR >= 0){                 // Price confirmed.
...
    else    CallAgain(price);                               // wait.
 

Cool! Ive never heard that saying before... definitely a little liberating and will stick with me!

Filtering ticks is a great idea too!

Thanks to you both!!

Reason: