multiple if or for loop with if inside

Hello. In terms of speed, which would be better?

for(byte i = 0; i < 15; i++){

if(array[i] == HIGH){
// do something...
}
}
or this
if(array[0] == HIGH){

}
if(array[1] == HIGH){

}
if(array[2] == HIGH){

}
....
until 15

Obviously the first one is faster to write :stuck_out_tongue:

The last one would theoretically run slightly faster, because it does not require a jump to the start of the loop, a variable manipulation and a compare to run before the next if statement is executed. However, these steps might compile down to 3 or d 4 extra commands, but the overall size of the program will be smaller, because it re-uses the same code.

In terms of actual execution time saved, you are looking at (4 * 15) commands at close to 16 million per second, so it saves about 4 microseconds off the processing time. not really worth it. If you are running that close to the wire in terms of the speed you need, then you probably need to get a faster processor.

thanks. I'm fine with the performance, but I was wondering because I wanted to optimize a part of my sketch, just because it's a bunch of ugly and LARGE if. It's a slighty different approach than that one. Here it goes, tell me what you think

if(array[15] == HIGH){
counterTest = 14;
}
if(array[15] == HIGH && array[14] == HIGH){
counterTest = 13;
}
if(array[15] == HIGH && array[14] == HIGH && array[13] == HIGH){
counterTest = 12;
}

... and so on until I have an inmensely big conditional. I needed to know which buttons are high from right to left(in the array) only when they are consequent. I didn't like that part of the code(but it was the first fix it came to mind) so tonight I made it "better" this way:

for(int i = 15; i >= 0; i--){ //scans the 16 buttons from higher to lower
     if(array[i] == LOW){  // from higher to lower, if it finds a button low, make the counterTest stop summing up
       stop = HIGH;
     
     }
     if(array[i] == HIGH){ //if it finds any that's high, increments the counter
       if(stop == LOW){
       counterTest++;
       }
     }
   }
stop = LOW; //reset the variables
counterTest = 0;

I like way better the latter code, and it may help me optimize other parts of the code with that counterTest value. But, in that case i'm running 2 if inside a for of 16 loops, so it's like 32 ifs..

capicoso:
thanks. I’m fine with the performance, but I was wondering because I wanted to optimize a part of my sketch, just because it’s a bunch of ugly and LARGE if. It’s a slighty different approach than that one. Here it goes, tell me what you think

if(array[15] == HIGH){

counterTest = 14;
}
if(array[15] == HIGH && array[14] == HIGH){
counterTest = 13;
}
if(array[15] == HIGH && array[14] == HIGH && array[13] == HIGH){
counterTest = 12;
}



... and so on until I have an inmensely big conditional.
I needed to know which buttons are high from right to left(in the array) only when they are consequent. I didn't like that part of the code(but it was the first fix it came to mind) so tonight I made it "better" this way:

That’s probably an unneccesary mess. Are the values in array other than 0 or 1?

Suppose that you wanted to make array bigger or smaller?

for(int i = 15; i >= 0; i--){ //scans the 16 buttons from higher to lower

if(array[i] == LOW){  // from higher to lower, if it finds a button low, make the counterTest stop summing up
       stop = HIGH;
     
     }
     if(array[i] == HIGH){ //if it finds any that’s high, increments the counter
       if(stop == LOW){
       counterTest++;
       }
     }
   }
stop = LOW; //reset the variables
counterTest = 0;



I like way better the latter code, and it may help me optimize other parts of the code with that counterTest value. But, in that case i'm running 2 if inside a for of 16 loops, so it's like 32 ifs..

Have you learned about else or break?

     if(array[i] == LOW)  // from higher to lower, if it finds a button low, make the counterTest stop summing up
     {
       stop = HIGH;  // if you don't need this after the loop then break takes care 
       break; // will exit the loop
     }
     else //if it's not LOW then it has to be HIGH, if LOW else HIGH
     { 
       counterTest++;
     }

If you read your digital pins that can only be LOW/HIGH (as 0/1) into the bits of an unsigned int then it becomes much much easier/faster to tell all kinds of things about the states of the buttons and relationships between them.

Bit math understanding will let you write powerful and fast code. Bit logic does all the bits in one command, few to one cycle depending on 8 or 16 bits and what’s already in the CPU registers. It can take days to weeks to get comfortable with but so can driving a car.

http://playground.arduino.cc/Code/BitMath

GoForSmoke:

capicoso: thanks. I'm fine with the performance, but I was wondering because I wanted to optimize a part of my sketch, just because it's a bunch of ugly and LARGE if. It's a slighty different approach than that one. Here it goes, tell me what you think

if(array[15] == HIGH){
counterTest = 14;
}
if(array[15] == HIGH && array[14] == HIGH){
counterTest = 13;
}
if(array[15] == HIGH && array[14] == HIGH && array[13] == HIGH){
counterTest = 12;
}

... and so on until I have an inmensely big conditional. I needed to know which buttons are high from right to left(in the array) only when they are consequent. I didn't like that part of the code(but it was the first fix it came to mind) so tonight I made it "better" this way:

That's probably an unneccesary mess. Are the values in array[] other than 0 or 1?

Suppose that you wanted to make array bigger or smaller?

for(int i = 15; i >= 0; i--){ //scans the 16 buttons from higher to lower
     if(array[i] == LOW){  // from higher to lower, if it finds a button low, make the counterTest stop summing up
       stop = HIGH;
     
     }
     if(array[i] == HIGH){ //if it finds any that's high, increments the counter
       if(stop == LOW){
       counterTest++;
       }
     }
   }
stop = LOW; //reset the variables
counterTest = 0;

I like way better the latter code, and it may help me optimize other parts of the code with that counterTest value. But, in that case i'm running 2 if inside a for of 16 loops, so it's like 32 ifs..

Have you learned about else or break?

     if(array[i] == LOW)  // from higher to lower, if it finds a button low, make the counterTest stop summing up
     {
       stop = HIGH;  // if you don't need this after the loop then break takes care 
       break; // will exit the loop
     }
     else //if it's not LOW then it has to be HIGH, if LOW else HIGH
     { 
       counterTest++;
     }

If you read your digital pins that can only be LOW/HIGH (as 0/1) into the bits of an unsigned int then it becomes much much easier/faster to tell all kinds of things about the states of the buttons and relationships between them.

Bit math understanding will let you write powerful and fast code. Bit logic does all the bits in one command, few to one cycle depending on 8 or 16 bits and what's already in the CPU registers. It can take days to weeks to get comfortable with but so can driving a car.

http://playground.arduino.cc/Code/BitMath

thanks, that looks better indeed. I forgot about break; ... and the else... well heh. I read about the bit math when I wanted to manipulate the bits of a byte. It scared me, and then I found about bitRead and bitWrite ... I will take a look...

Break is a good solution to your stop variable, however as the code is fairly straight forward, we can do it with out any if statements at all:

char counterTest = 0;
for(int i = 16 ; i && array[i-1] ; --i, ++counterTest );

If you enable C++11 in IDE 1.5.7, you could also do this:

for( auto i : array ){
  if( i == LOW ) break;  
  ++counterTest;
}

If those array[ ] items are going to be made "high" by switches or buttons or any kind of event happening, then the quickest method is to keep track of how often these events occur, and then in your loop which looks for occurences of the event happening, look for the most popular events first.

pYro_65: Break is a good solution to your stop variable, however as the code is fairly straight forward, we can do it with out any if statements at all:

char counterTest = 0;
for(int i = 16 ; i && array[i-1] ; --i, ++counterTest );

That is neat.

I'd be needing to crack a book for a good while to catch up to C++11, for sure. :D

GoForSmoke: That is neat.

I'd be needing to crack a book for a good while to catch up to C++11, for sure. :D

Cheers.

For quick entry to C++11, searching google for the topics listed in the following link will probably help out: https://gcc.gnu.org/gcc-4.8/cxx0x_status.html

pYro_65: Break is a good solution to your stop variable, however as the code is fairly straight forward, we can do it with out any if statements at all:

char counterTest = 0;
for(int i = 16 ; i && array[i-1] ; --i, ++counterTest );

If you enable C++11 in IDE 1.5.7, you could also do this:

for( auto i : array ){
  if( i == LOW ) break;  
  ++counterTest;
}

Nice... could you point me where I could read an example of that for syntax? I don't understand the && array[i-1] and the comma before ++counterTest.

capicoso:

pYro_65: Break is a good solution to your stop variable, however as the code is fairly straight forward, we can do it with out any if statements at all:

char counterTest = 0;
for(int i = 16 ; i && array[i-1] ; --i, ++counterTest );

If you enable C++11 in IDE 1.5.7, you could also do this:

for( auto i : array ){
  if( i == LOW ) break;  
  ++counterTest;
}

Nice... could you point me where I could read an example of that for syntax? I don't understand the && array[i-1] and the comma before ++counterTest.

i && array[i-1] ..... the loop runs as long as this is true. && is a logical as opposed to bitwise AND

Look about halfway down the Reference on the left side for Boolean and Bitwise Operators http://arduino.cc/en/Reference/HomePage

--i, ++counterTest ........ it's a comma separated list of actions done when the loop loops

GoForSmoke:

capicoso:

pYro_65: Break is a good solution to your stop variable, however as the code is fairly straight forward, we can do it with out any if statements at all:

char counterTest = 0;
for(int i = 16 ; i && array[i-1] ; --i, ++counterTest );

If you enable C++11 in IDE 1.5.7, you could also do this:

for( auto i : array ){
  if( i == LOW ) break;  
  ++counterTest;
}

Nice... could you point me where I could read an example of that for syntax? I don't understand the && array[i-1] and the comma before ++counterTest.

i && array[i-1] ..... the loop runs as long as this is true. && is a logical as opposed to bitwise AND

Look about halfway down the Reference on the left side for Boolean and Bitwise Operators http://arduino.cc/en/Reference/HomePage

--i, ++counterTest ........ it's a comma separated list of actions done when the loop loops

thanks. Last question, why is the -- and ++ in the left side of the variable instead of the right?

In those cases it doesn't matter but

i = 5;

x = --i; // i is pre-decremented here, i = i - 1 before i is copied to x // x is now 4 and i is now 4

y = i--; // i is post-decremented here, i = i - 1 after i is copied to y // y is now 4 and i is now 3