if statement | Optimum Performance

 

Hi Friends

I have a confusion which One of the following "if statements set" is better in terms of CPU performance?   

If statement set [A]

                if(STTH01[k+1].fastTrend == SHORT && STTH01[k].fastTrend == LONG) {
                        mPriceSL = STTH01[k].fast;
                        OpenPOSITION(ORDER_TYPE_BUY,mPriceSL,mPriceTP,mMagicGUGD,mComment);
                        return;
                }       else
                if(HMAH01[k].trend == LONG && STTH01[k].slowTrend == LONG) {
                        if(STTM15[k+1].fastTrend == SHORT && STTM15[k].fastTrend == LONG) {
                                mPriceSL = STTM15[k].fast;
                                OpenPOSITION(ORDER_TYPE_BUY,mPriceSL,mPriceTP,mMagicGUGD,mComment);
                                return;
                        }
                } else
                if(HMAH01[k].trend == LONG && STTH01[k].fastTrend == LONG) {
                        if(STTM15[k+1].fastTrend == SHORT && STTM15[k].fastTrend == LONG &&  STTM15[k].slowTrend == LONG) {
                                mPriceSL = STTM15[k].fast;
                                OpenPOSITION(ORDER_TYPE_BUY,mPriceSL,mPriceTP,mMagicGUGD,mComment); 
                                return;
                        }
                }

If statement set [B]

                bool posOpen = false;
                if(STTH01[k+1].fastTrend == SHORT && STTH01[k].fastTrend == LONG) {
                        mPriceSL = STTH01[k].fast;
                        posOpen = true;
                }       else
                if(HMAH01[k].trend == LONG && STTH01[k].slowTrend == LONG) {
                        if(STTM15[k+1].fastTrend == SHORT && STTM15[k].fastTrend == LONG) {
                                mPriceSL = STTM15[k].fast;
                                posOpen = true;
                        }
                } else
                if(HMAH01[k].trend == LONG && STTH01[k].fastTrend == LONG) {
                        if(STTM15[k+1].fastTrend == SHORT && STTM15[k].fastTrend == LONG && STTM15[k].slowTrend == LONG) {
                                mPriceSL = STTM15[k].fast;
                                posOpen = true;
                        }
                }

if(posOpen) {
        OpenPOSITION(ORDER_TYPE_BUY,mPriceSL,mPriceTP,mMagicGUGD,mComment);
        return;
}

In my opinion set [B] should be better, please advise.

 
I would speculate it does not matter, either version should perform as fast. The compiler should produce in both cases the same or at least equivalent machine code.

But, measure it, I would suggest. This way you can be sure.

Edit:

If you really want to reduce the amount of operations, then you will need to restructure the if-clauses, because you are asking the same question multiple times.

For a code to be consistently fast, it is best if, no matter the input parameters, the sequence of operations doesn't change.

Instead of using conditional blocks like right now, you could make use of ternary operator " () ? :" and accumulate a "state-variable" which you then use in a final decision block to execute or skip. (Could be a switch or an if-statement)

You cannot always have a fixed sequence, but the more often you do manage to avoid a conditional block, the more the Pipeline of your CPU is saturated.
 
Dominik Christian Egert #:
I would speculate it does not matter, either version should perform as fast. The compiler should produce in both cases the same or at least equivalent machine code.

But, measure it, I would suggest. This way you can be sure.

Edit:

If you really want to reduce the amount of operations, then you will need to restructure the if-clauses, because you are asking the same question multiple times.

For a code to be consistently fast, it is best if, no matter the input parameters, the sequence of operations doesn't change.

Instead of using conditional blocks like right now, you could make use of ternary operator " () ? :" and accumulate a "state-variable" which you then use in a final decision block to execute or skip. (Could be a switch or an if-statement)

You cannot always have a fixed sequence, but the more often you do manage to avoid a conditional block, the more the Pipeline of your CPU is saturated.

Thanks a lot @Dominik Christian Egert.

"Instead of using conditional blocks like right now, you could make use of ternary operator " () ? :" and accumulate a "state-variable" which you then use in a final decision block to execute or skip. (Could be a switch or an if-statement)"

This is valuable suggestion though I have never tried it and just seen them on custom indicators. I will dig deeper into it and revise my code.

If statement look simpler in using :) so got used to it.

 

I would say A is faster, just because in B you have an extra set variable (the boolean), but it's not very significant. If you want to confirm that, run the profiler (but it will probably highlight other areas of your code)

As a suggestion, if you return in an if clause you don't need to put the "else", anything outside and below is only reached if it doesn't enter the first condition (those would be called "guard clauses").

You can also rearrange your code as Dominik said to avoid repeating calculations

if(STTH01[k+1].fastTrend == SHORT && STTH01[k].fastTrend == LONG) {
   mPriceSL = STTH01[k].fast;
   OpenPOSITION(ORDER_TYPE_BUY,mPriceSL,mPriceTP,mMagicGUGD,mComment);
   return;
}

if (HMAH01[k].trend != LONG)
        return;

if (STTM15[k+1].fastTrend != SHORT)
        return;

if (STTM15[k].fastTrend != LONG)
        return;

if (STTH01[k].slowTrend == LONG && STTM15[k].fastTrend == LONG) {
   mPriceSL = STTM15[k].fast;
   OpenPOSITION(ORDER_TYPE_BUY,mPriceSL,mPriceTP,mMagicGUGD,mComment);
   return;
}

if(STTH01[k].fastTrend == LONG && STTM15[k].slowTrend == LONG) {
   mPriceSL = STTM15[k].fast;
   OpenPOSITION(ORDER_TYPE_BUY,mPriceSL,mPriceTP,mMagicGUGD,mComment); 
   return;
}

You can reverse the conditions and make them guard clauses too (especially those that repeat). Those fragments I highlighted do the same so probably they could also be extracted.


If you want to really improve the performance, make sure that the conditions that are more likely to fail (so you would exit the function) are first in order (that's something you would need to test for)

 
Manuel Alejandro Cercos Perez #:

I would say A is faster, just because in B you have an extra set variable (the boolean), but it's not very significant. If you want to confirm that, run the profiler (but it will probably highlight other areas of your code)

As a suggestion, if you return in an if clause you don't need to put the "else", anything outside and below is only reached if it doesn't enter the first condition (those would be called "guard clauses").

You can also rearrange your code as Dominik said to avoid repeating calculations

You can reverse the conditions and make them guard clauses too (especially those that repeat). Those fragments I highlighted do the same so probably they could also be extracted.


If you want to really improve the performance, make sure that the conditions that are more likely to fail (so you would exit the function) are first in order (that's something you would need to test for)

Dear @Manuel Alejandro Cercos Perez thanks a lot for your valuable suggestions to improve my coding style on if..loop.

'The Guard Clauses" suggestion is something to make if..loop more clear.

I usually have if(!(condition))  return; to filter (guard clauses) for early exit of functions or methods. However still sometimes true or false conditions are so many that makes me use above style of code writing.

I made some other improvements as below:

Instead of using below condition, I have incorporated the the value in STTH01 Structure and call the GetArrayMethod gets it done for 'n' bars at PERIOD_H1 intervals. For position entry condition, I simply fetch value from structure at IsNewBar() of any given time frame or it can be every tick.

STTH01[k+1].fastTrend == SHORT && STTH01[k].fastTrend == LONG
Changed to STTH01[k].isCoT == LONG ... 

I am sure suggestions by you and Dominik, will go long way in improving my code writing.

Regards.

 

Please don't post randomly in any section. Your topic has been moved to the section: Expert Advisors and Automated Trading

Please don't create topics randomly in any section.

 
Fernando Carreiro #:
Please don't post randomly in any section. Your topic has been moved to the section: Expert Advisors and Automated Trading

Hi @Fernando Carreiro

Well I am under the impression that once a post is moved to specific section, if I use 'reply' it will come from new section.

Please guide me how to make sure that replies are going from new moved section.

regards

 
Replies go to the thread, where ever the thread currently is.
 
William Roeder #:
Replies go to the thread, where ever the thread currently is.
Hi @Fernando Carreiro


My assumptions is confirmed by William. May be you have seen a post before it was transferred to another section or may you just had a bad day :) while writing your post.

 
Anil Varma #: Hi @Fernando Carreiro My assumptions is confirmed by William. May be you have seen a post before it was transferred to another section or may you just had a bad day :) while writing your post.
No, I was referring to your first post (the thread itself) which was in the "Trading Systems".
 
Fernando Carreiro #:
No, I was referring to your first post (the thread itself) which was in the "Trading Systems".

Ohh, ok 

Is there any guideline which I can refer to find correct forum for my post?

thanks

Reason: