This will not be a very useful tip for 99% of your arduino projects, but if you are wanting to keep your code running as quick as possible without reverting to C its a handy tip to have.

 

Ive got a few arduino projects going now where I need to keep my main loops running fast, This is a test to verify that a multiple IF statement finished if the primary statement is false.

 

In example...  if(A= =false && B== true)  the arduino will move onto the next line of code if stament A is not true. Restructuring your code a little to make sure you ask a boolean statement first can give you a little quicker loop times.

 

The results from the below test code:

Structure 1 :

24 (ms)

Structure 2 :

16 (ms)

 

Not a massive increase but something to keep in mind if you have resource expensive stamen conditions.

 

 

The Test Code:

 

The Code

/*

Script to verify how the arduino habldes if routines with multiple statments. ABA test

 

 

   28/8/2015  Michael Ratcliffe  Mike@MichaelRatcliffe.com

  

  

          This program is free software: you can redistribute it and/or modify

    it under the terms of the GNU General Public License as published by

    the Free Software Foundation, either version 3 of the License, or

    (at your option) any later version.

 

 

    This program is distributed in the hope that it will be useful,

    but WITHOUT ANY WARRANTY; without even the implied warranty of

    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

    GNU General Public License for more details.

 

 

    You should have received a copy of the GNU General Public License

    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

 

 

 

 

 

 

 

 

//Just some variables for the test

 

 

long start=0;

int i=0;

int flag=1;

 

 

void setup() {

  // put your setup code here, to run once:

Serial.begin(9600);

 

 

 

 

//Test A, a boolean comparison fisrt

//runs loop 10,000 times and makes a log of time to compute if statment, shoot results out to terminal

i=0;

start=millis();

while (i<10000) {

if(millis()%(6000)>10000000 && flag==0);

i++;

}

Serial.println("Structure 1 : ");

Serial.println(millis()-start);

 

 

//Test B, a heavier math based statment first

i=0;

start=millis();

while (i<10000) {

if( flag==0 && millis()%(6000)>10000000 );

i++;

}

Serial.println("Structure 2 : ");

Serial.println(millis()-start);

 

 

//Test A, a boolean comparison fisrt

i=0;

start=millis();

while (i<10000) {

if(millis()%(6000)>10000000 && flag==0);

i++;

}

Serial.println("Structure 1 : ");

Serial.println(millis()-start);

 

 

 

}

 

 

void loop() {

//We only need to run the test once

};