Which is the most efficient?

 

Which is the most  efficient programing? Reading and "if" statement every tick or just setting the bool variable every tick?  Kind Regards  ---Tom

if(rrPrinted) rrPrinted = false;   ||  rrPrinted = false;      Most efficient?


    // CALCULATE PERCENT OF PIPS RISK 
    if( EAOpenTrades() > 0)
     {
       if(rrPrinted) rrPrinted = false;     // <<<<<<<<<<======== 
       
       CalculateRR();
       Text[2] = "RR: " + DoubleToStr( RR,2);   
       ObjectSetText("RR", Text[2],10,"Arial",TextColor);
     }
    else
     {
       if(!rrPrinted){Print( "Reward/Risk = ",DoubleToStr( RR,2) ); rrPrinted = true;}
       
       Text[2] = "RR:  ---  %";   
       ObjectSetText("RR", Text[2],10,"Arial",TextColor);
     }
 
tparimore: Which is the most  efficient programing? Reading and "if" statement every tick or just setting the bool variable every tick?  Kind Regards  ---Tom
Obviously, just setting it will be more efficient.
 
Fernando Carreiro #:
Obviously, just setting it will be more efficient.

Just to be clear. When I say more efficient programing, I don't mean less code. I 'm talking fastest running code. I kind of think they will be the same. It just seems wrong to set rrPrinted over and over again.  Thanks for your reply!  ---Tom 

 
Benchmark
Benchmark
  • www.mql5.com
Измерение времени выполнения функций/методов, выражений и частей кода.
 
tparimore #: Just to be clear. When I say more efficient programing, I don't mean less code. I 'm talking fastest running code. I kind of think they will be the same. It just seems wrong to set rrPrinted over and over again.  Thanks for your reply!  ---Tom 

Yes, my answer was based on resource efficiency (less CPU usage).

Even if writing to RAM is somewhat slower that reading from RAM, it is done in the background, while the CPU continues to do its thing. So a Read Cycle or a Write cycle takes practically the same amount of time.

However, for your first option you are reading, then comparing. That will take more cycles then just writing it always and nothing else.

That being said, even a "write" can sometimes be one cycle to many, so see if you can rethink your overall logic to eliminate the need to set the variable in the first place.

 
tparimore:

Which is the most  efficient programing? Reading and "if" statement every tick or just setting the bool variable every tick?  Kind Regards  ---Tom


Well I think It gets down to what the rest of your code does; if you have frequent occasions when rrPrinted is set to true, then it will be economical to skip the check. Otherwise, the difference will be negligible.


 
Fernando Carreiro #:

Yes, my answer was based on resource efficiency (less CPU usage).

Even if writing to RAM is somewhat slower that reading from RAM, it is done in the background, while the CPU continues to do its thing. So a Read Cycle or a Write cycle takes practically the same amount of time.

However, for your first option you are reading, then comparing. That will take more cycles then just writing it always and nothing else.

That being said, even a "write" can sometimes be one cycle to many, so see if you can rethink your overall logic to eliminate the need to set the variable in the first place.

That makes sense. Thank you.

As far as rethinking the overall logic, I can't think of a better way to Print only the last RR a once trade has closed. Maybe you have some ideas? 

Once in a trade (EAOpenTrades() > 0), CalculateRR() tracks (variable RR) the % gain/loss compared to the pips risked i.e. stoploss.  A -0.5 means I am halfway to my SL. A +1.5 means I am in profit 1.5 times my risk.  When trade is closed, I want to print the last RR.

Thanks again,

Tom

 

I'd go for initializing the variable inside the "if". There's nothing wrong in that, and you can see that very often whether it is initializing an int, string or whatever. Maybe the difference is insignificant, but it kind of feels very redundant and unnecessary asking every single time instead of just initializing the bool to false. Maybe it's just my coding style.

Regards.

 
tparimore #: As far as rethinking the overall logic, I can't think of a better way to Print only the last RR a once trade has closed. Maybe you have some ideas? 

We don't have access to your overall code logic or know and understand your objectives, so it would be difficult to offer other viable options.

Just keep the idea in your mind and as you progress with your development, you might come up with an alternative. For now, I don't think it will be that critical to just leave it as is.

Reason: