# millis() counter speed

Hi all, I have a portion in my code that I only want to execute once in a while. So I have two type of solutions.

Use a counting variable in the loop which counts to a specific level until the Do Stuff is executed.

``````void loop()
{
Counter ++;
if (Counter > 1000){
// Do Stuff
Counter = 0;
}
}
``````

Or have a millis() function which compares the millis() versus the olf millis() value to determine the Do Stuff moment.

``````void loop()
{
Counter = millis();
if ((millis() - Counter) > 1000){
// DoStuff
}
}
``````

Now I know both solutions work but what I wonder is which is the fastest, meaning which of the two slows down the code the least (uses the least processor power)

Many thanks! Steven

The least number of CPU cycles is likely to be:

``````unsigned Counter = 0;
void loop()
{
if (Counter++ > 1000)
{
Counter = 0;
// DO STUFF
}
}
``````

That won't save any power over any other solution that does not involve a sleep mode. The advantage of the millis() version is that you can specify exactly how often you want to DO STUFF. The counter version will depend on how long it takes to do whatever else is happeneing in loop(). Are you trying to save battery power? If so you will have to use sleep.

The second method allows you to run alongside other concurrent tasks in your sketch - in other words it isn't hogging the processor.

I have tried the 3 methods for a rough speed comparisons. And here are the KHZ results:

The below code made a 533KHZ "square wave"

``````#include <digitalWriteFast.h>
int Counter = 0;
void setup()
{
pinMode(52, OUTPUT);
}

void loop()
{
Counter ++;
if (Counter > 100){
// Do Stuff
Counter = 0;
}
digitalWriteFast(52, HIGH);
digitalWriteFast(52, LOW);
}
``````

The below code made a 241KHZ "square wave"

``````#include <digitalWriteFast.h>
int Counter = 0;
void setup()
{
pinMode(52, OUTPUT);
}

void loop()
{
Counter = millis();
if ((millis() - Counter) > 100){
// DoStuff
}
digitalWriteFast(52, HIGH);
digitalWriteFast(52, LOW);
}
``````

The below code made a 500KHZ "square wave"

``````#include <digitalWriteFast.h>
int Counter = 0;
void setup()
{
pinMode(52, OUTPUT);
}

void loop()
{
if (Counter++ > 100)
{
Counter = 0;
// DO STUFF
}
digitalWriteFast(52, HIGH);
digitalWriteFast(52, LOW);
}
``````

So all in all it seems the millis function is about 2x as slow as the variable counter. Although obviously the variable counter is depending on the rest of the code.

Regards,
Steven

I suspect we are comparing (counting) oranges and apples here

the millis() solution will wait roughly the number of milliseconds in the test (100 in your timed example) whereas anything based on counter++ will just iterate the counter a number of times (again 100 in your timed example)

now unless counter++ takes 1 millisecond (which would be remarkably long) I would expect that technique to be oodles faster (hmm what datatype does oodle() return?)

so it depends what you are trying to do here a) delay a fixed time b) count a number of iterations

I hope that makes sense (it did in my head when I wrote it)

There’s also `micros()` don’t forget…

One difference is that this version will never “Do Stuff”.

``````#include <digitalWriteFast.h>
int Counter = 0;
void setup()
{
pinMode(52, OUTPUT);
}

void loop()
{
Counter = millis();
if ((millis() - Counter) > 100){
// DoStuff
}
digitalWriteFast(52, HIGH);
digitalWriteFast(52, LOW);
}
``````

This version WILL ‘Do Stuff’ every 100 milliseconds AND it will loop faster:

``````#include <digitalWriteFast.h>
unsigned long Counter = 0;

void setup()
{
pinMode(52, OUTPUT);
Counter = millis();
}

void loop()
{
if ((millis() - Counter) > 100){
// DoStuff
Counter = millis();
}
digitalWriteFast(52, HIGH);
digitalWriteFast(52, LOW);
}
``````

Thanks all!

At Johnwasser, Whoop my bad, yes that code is indeed a bit faster.