Is it possible to avoid many "ors" (||) in conditions causing the same action? - page 2

 
TarasBY:

You can also optimise calculations in this way:

Thanks! I was already advised to use bool Avals, and Vinin warned me that it would seriously slow down. And after I've almost finished an EA I'm looking for something else to optimize in code. I've already managed to reduce the run in the tester on all ticks in 12 months from 40 minutes to 13!
 

It has been said many times: there is no shortcut in MCL, if(a||b|||c||d||e){} all conditions will be checked.

The way out is nesting conditions:

if(a){if(b){if(c){}}} in which case the exit will occur at the first matching condition

But nesting is not infinite either, the most flexible solution is to check the conditions in a loop with an exit :

bool cond[]={a,b,c,d,e,...};
int sz = ArraySize(cond);
for(int i=0;i<sz;i++){
   if(cond[i]){
      bla.bla.bla;
      break;
   }
}
 
Vinin: You can, but the speed drops a lot.
Victor, do your warnings (about using a bool variable) apply to my optimization options?!
 
FAQ:

It has been said many times: there is no shortcut in MCL, if(a||b|||c||d||e){} all conditions will be checked.

The way out is nesting conditions:

if(a){if(b){if(c){}}} in which case the exit will occur at the first matching condition

But nesting is not infinite either, the most flexible solution is to check the conditions in a loop with an exit :

Thank you very much! I'll give it a try! Do you know any Spanish? Because "bla, bla, bla" comes from the verb "to speak" (hablar)!
 
No, but I'm good at Olban :))
 
FAQ:
No, but I know Olbanian well :))

Nesting conditions if(a){if(b){if(c){}}} is not good, because all conditions are mutually exclusive, but something does not work with boolean. Probably and most likely I'm doing something wrong. So far I've stopped at making a variable double for each condition and pasting it into the same if() with 4 "or". As I expected, it hasn't affected speed of testing. Same 13 minutes on all ticks for 12 months.

Or maybe this language is calledOblansky?

 
borilunad:

Nested conditions if(a){if(b){if(c){}} are not good, because all conditions are mutually exclusive. Probably and most likely doing something wrong.


All is good, just remember some identities from Boolean arithmetic, for example: a || b = !( !a && !b ). Then by changing from conjunction to disjunction we can replace check of OR condition by consecutive (nested) check of AND conditions: for example, if (a || b || c) turns, as TarasBY has rightly written above, into

bool M = true;

if(!a) {if(!b) {if(!c) M=false;}}

if(!M) {Action;}

This code is in general faster than the original code with ||, because it is executed only until one of the conditions in the list is true, i.e. until the whole OR operation results in TRU.

 
alsu:

All is good, just remember some identities from Boolean arithmetic, for example: a || b = !( !a && !b ). Then by changing from conjunction to disjunction we can replace check of OR condition by consecutive (nested) check of AND conditions: for example, if (a || b || c) turns, as TarasBY has rightly written above, into

This code is in general faster than the original code with || because it is executed only until one of the conditions in the list is true, i.e. until the whole OR operation yields a TRU result.

We can also speed up, for example, if we know that condition A is executed on average more often than C, and C more often than B, then we should put them in that order: if(!a){if(!c)if{(!b) M=false;}}
 
alsu:

All is good, just remember some identities from Boolean arithmetic, for example: a || b = !( !a && !b ). Then by changing from conjunction to disjunction we can replace check of OR condition by consecutive (nested) check of AND conditions: for example, if (a || b || c) turns, as TarasBY has rightly written above, into

This code is in general faster than the original code with || because it is executed only until one of the conditions in the list is true, i.e. until the whole OR operation yields a TRU result.

Thank you for your participation! I have 5 different but equal in value conditions, and if no condition is true, the EA waits until one of them is true. Therefore none of the forms of implementation proved to be better, in spite of my efforts. Therefore, I will stick with my "pies" for now.
 
borilunad:
Thank you for your participation! I have 5 different but equal conditions, and if no condition is met, the EA waits for one of them to be met. Therefore, no single form of implementation has proved to be better, despite my efforts. Therefore, I will stick with my "pies" for now.
Even what I just showed you? Well, then the performance should be improved by something else.