optimization of code - page 2

 
Fernando Carreiro:
OK! If I some free time tomorrow, I will try to write some code to demonstrate it!

I'd be interested if you can actually write such a thing.

I'm fairly sure you could not using a decent C++ or C# compiler

 

Run this a few times and you'll see there is nothing in it really.

#property strict

void OnStart()
  {
   int x=1,
       y=2;

   ulong begin=GetMicrosecondCount();  
   for(int i=0; i<10000000; i++)
     {
      if(x==1 && y==1) { Print("This should never print"); }
     }
   printf("&& took %iμs",GetMicrosecondCount()-begin);
  
   begin=GetMicrosecondCount();  
   for(int i=0; i<10000000; i++)
     {
      if(x==1)
        {
         if(y==1) { Print("This should never print"); }
        }
     }
   printf("Nested 'if' statements took %iμs",GetMicrosecondCount()-begin);
  }  


 

 
James Cater:

I'd be interested if you can actually write such a thing.

I'm fairly sure you could not using a decent C++ or C# compiler

 
Well, if everyone (Alain Verleyen, WHRoeder, James Carter, honest_knave) says I am wrong , then I MUST be wrong!
 
Alain Verleyen:

I was understanding "optimize", in the sense of performance, so faster code. In this sense there is no point to use "if()" instead of "&&".

As far as I remember old versions of mql4 checked the 'whole line' of: if ( a && b && c && ( d || e) ).

In this case if (a) { if (b) { .. would be faster - indeed!

But now in case a == false if is false as well and the other part is not checked any more so there is no advantage any longer.

 
Carl Schreiber:

As far as I remember old versions of mql4 checked the 'whole line' of: if ( a && b && c && ( d || e) ).

In this case if (a) { if (b) { .. would be faster - indeed!

But now in case a == false if is false as well and the other part is not checked any more so there is no advantage any longer.

Yes, this is correct. From the MQL4 Reference (also linked to by WHRoeder):

 

Shortened conditions check is now used in logical operations, unlike the old MQL4 version where all expressions have been calculated and the check has been performed afterwards. Suppose there is a check of two conditions with the use of logical AND:

  if(condition1 && condition2)
    {
     // some block of statements
    }

If condition1 expression is false, calculation of condition2 expression is not performed, as false && true result is still equal to false.

 

Thank for your replies.

I want to optimize my code, because backtests take too much time. I found out that is because of use of "icustom"

in icustom im using ATR little modified to show me median instead of avg. I made little change to decrease significantly amount of operation like bellow, but after that my EA doesnt make trades any more whereas the indicator visually works fine, just shows recent few (=ATRperiod+1) values

//--- the main loop of calculations
   for(i= rates_total-limit; i<rates_total; i++)            ////// originally: for(i=limit; i<rates_total; i++) /////  
     {
      ExtTRBuffer[i]=high[i]-low[i];
    
      int n= MathRound((InpAtrPeriod-1)/2);
     double Ext[];
     ArrayResize(Ext,InpAtrPeriod-1);
     for(j=0;j<InpAtrPeriod-1;j++)
     { Ext[j]=ExtTRBuffer[i+j+1-InpAtrPeriod] ; }
     ArraySort(Ext);
      ExtATRBuffer[i]= (Ext[n]+Ext[n-1]+Ext[n+1])/3;
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

Plz help

edit// ok i see the indicator doesnt work
Reason: