CPU Efficiency

 

From the point of view of computer efficiency and resources, if I want to find the maximum of 3 real numbers, Val1, Val2 and Val3, should I use two sets of IF statements:

if( Val1 > Val2)

{

if( Val1 > Val3) Maximum = Val1;

else Maximum = Val3;

}

else if( Val2 > Val3) Maximum = Val2;

else Maximum = Val3;

OR two sets of MathMax() functions:

Maximum = MathMax( Val1, Val2);

Maximum = MathMax( Maximum, Val3);

 

The most definitive answer you can get is to run those 2 pieces of code on your computer, and time them.

Obviously, running them once is not going to tell you anything. Try running them a few zillion times in a loop until you get an answer.

 
Maximum = MathMax( MathMax(Val1, Val2), Val3 );

The IF version may be slightly more efficient since it avoids two function calls, but the above is definitely more readable. Don't optimize code, first get it to work, then only if you find a large bottlenext should you consider timing, modify, and retime.

You'll get infinitely more bang for the buck if you simply filter out useless ticks, than worrying about such minutiae.

//+------------------------------------------------------------------+
//| Skip useless ticks                                               |
//+------------------------------------------------------------------+
double  CA.below,   CA.above=0;         // Export to start
void    CallAgain(double when, string why){
//  static string below="", above="";
    if (when>=Bid && CA.above > when){  CA.above = when;    }// above=why;  }
    if (when<=Bid && CA.below < when){  CA.below = when;    }// below=why;  }
//Print(below," ",PriceToStr(CA.below),"<CA<",PriceToStr(CA.above)," ",above,
//  " (", why, " ", PriceToStr(when),")");
    return;
}
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int     start(){
    #define INF 0x6FFFFFFF      // Not quite infinite, Jul 2029, or 1,879,048,191
    static datetime Time0;  bool newBar = Time0 != Time[0];
    if (newBar || Bid < CA.below || Bid > CA.above){  // Important
        CA.below = 0;   CA.above = INF;     Time0 = Time[0];
...
    if ((Bid-pattern.trigger)*DIR <= 0.0){                      // Still below.
        CallAgain(pattern.trigger,"pt3");                       // Wait.
            return;
    }
See other examples here
 

Hi blogzr3 and WHRoeder,

Thanks for your replies

 

And of course, if you need to run a zillion times to tell the difference, the answer is it's not enough to worry about.

The question then becomes what is relevant and what is not. There isn't a rule book that lists everything in order, unfortunately, so the answer lies in a mixing of testing, timing and head-scratching, and not necessarily in any order.

 

FYI, I ran the two loops 100 million times and the IF statements took 1 second whereas the function calls took 8 seconds.