element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Vertical Farming
  • Challenges & Projects
  • Design Challenges
  • Vertical Farming
  • More
  • Cancel
Vertical Farming
Blog Automated Green House Blog:9.3 - Tip for trimming arduino If statement times
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: m.ratcliffe
  • Date Created: 12 Feb 2016 5:59 PM Date Created
  • Views 785 views
  • Likes 3 likes
  • Comments 1 comment
  • arduino:
  • adapted_greenhouse
Related
Recommended

Automated Green House Blog:9.3 - Tip for trimming arduino If statement times

m.ratcliffe
m.ratcliffe
12 Feb 2016

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

};

  • Sign in to reply

Top Comments

  • mhmayyan
    mhmayyan over 9 years ago +1
    Really nice test. It is new for me. This timing issue would be clear if I programmed in assembly but I have never thought in this way with programming in C/C++. Thanks,
  • mhmayyan
    mhmayyan over 9 years ago

    Really nice test. It is new for me.

     

    This timing issue would be clear if I programmed in assembly but I have never thought in this way with programming in C/C++.

     

    Thanks,

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube