True speed difference in loop() between R3 and R4

I have been trying to determine how many times code is actually run in the loop() in a second. So i wrote this simple code to count the loops. I am not sure if i did something wrong in the code but I'm getting a positive 1495998 on the Uno R4 WiFi and a negative -28140 using the same code on the Uno R3. It makes absolutely no sense to me. I have tried if statements and now a while statement to run the control structure inside the loop. I've even tried to initialize the Value variable to 1 so that i don't use 0 in the calculations. Can any one please help me. I feel even more ignorant than usual.

Here is the code i used

// Initialize variables
unsigned long startTime;
int Value = 0;                  // I've tried 1 as well

void setup() {
  // Initialize serial communication
  Serial.begin(9600);
  // Initialize the start time
  startTime = millis();
}

void loop() {
  // Check if 1 second has elapsed, tried from 0 to 1000 but got smaller count numbers
  while (millis() - startTime >= 1000 && millis() - startTime < 2000) {
    // Increment Value
    Value++;
  }
  
  // 2 seconds have elapsed, print the count and stop
  if (millis() - startTime >= 2000) {
    Serial.print("Value count in 1 second: ");
    Serial.println(Value);

    // Stop further execution
    while (true) {
      // Do nothing, loop indefinitely
    }
  }
}

Welcome to the forum

int Value = 0; // I've tried 1 as well

An int is a signed variable. Try using an unsigned int instead

Declare Value as unsigned long.

Any time you have unexpected negative values, always think "overflow".

Thank you so much, now I get 37397 loops a second in stead of -28139. do you think it's a smaller number because of the amount of processing involved

Thanks for your attempt to use code tags; I've fixed them. Code tags are ```, not ''' :wink:


An int on the R4 is 32bit, on the R3 16bit.

1 Like

"unsigned int" instead of "long" may give you a different result :slight_smile:

Could be, but I guess also whenever you get a number smaller than you expect, think "overflow?".

It is possible you have more than 65535 loops per second.

Its a smaller number on the R3 because of the 8-bit processor running at 16MHz, while the R4 has a 32-bit processor running at 48MHz.

< edit >
You are also not measuring the number of times loop() executes, you are measuring how many times the while loop executes, which in this case is highly dependent on how long millis() takes to execute.

Sure, but then you won't get an overflow and be happy :sunglasses:
Joke aside, what's the point in determing the loop()s/second ?

Im so sorry sterretje, I'm new to Arduino and using the forum, still like a little kid trying to walk without knowing in what direction. Every time I start to think I know anything, I suddenly get pulled down when realizing how little I actually know. I do not know how you guys cram all this coding information into a single human scull. My brain feel full and I know I don't know anything yet, Ignorance is a bliss but annoying to others.

Just FYI, on my Uno clone I get :

Value count in 1 second: 204003

I just find stuff like that interesting, so no actual reason. I think I'm just in awww about how fast controllers actually are. I got numbers like 1495999 out of the R4 and now 37397 out of the R3. Thats just amazing.

Is it a removable atmega328p chip?

I could remove it, but it is not in a socket :slight_smile:

I know the feeling. Although I have been programming in various languages and for different platforms for 30 years or so, I still learn by hanging around on this forum.

Thank you, I realize that but could not think of any other way to just try and get a benchmark test for determining how many times the loop() function actually run. Can you perhaps think of a better way david? I'm always open to learning better ways

Lol one of my subjects at varsity was informatika in 1987-90. Windows 3.11 did not even exist in the main stream then. It was part of a Marketing degree, so I became a farmer, Planting veggies for the past 30 years did not help me at all. But arduino is amazing, i spend all my time trying to make things now that could make it easier for people doing organic farming

The speed at which loop() executes is totally dependent on the code executed inside the loop.

That totally make sense, I just thought the crystal hz speed would have some kind of direct correlation to how many times the code execute. Mabe I should stop wasting time thinking about this type of nonsense and rather open a book and continue learning.

I think that's not the point here. Arduino does some bookkeeping in main() and calls loop() from there, so it gives you a feeling what's happening outside. That is highly depending on the arduino frameworks implementation on your hardware.
It does not help much, as you cannot change anything, but e.g. taking an Uno and compare barebone Atmega328p and Arduino makes you wonder.