For(....) loop seems doesn't work, I mean it doesn't take time

Hello everyone. I decided a speed test for some func. vs other.
And then write something simple program for looking for how long it takes.
My board is UNO 16 mhz. all okay. the problem is for loop doesn't run correctly i thing.
What's wrong ? thanks.

this is my sketch:

   created 25 kas 2021
  by   *****  *****
     This example code is in the public domain.
     speed test  if(    vs   min(
#include <Arduino.h>
//#include <math.h>

unsigned long pv_time = 0;
//uint16_t  testval = 0;

void setup() {
  // put your setup code here, to run once:
  // initialize serials:
  // debug ve proglamlama portu.
  Serial.println("Start ... ");


void loop() {
  // put your main code here, to run repeatedly:

  // * * * * * *
  pv_time  = millis(); // proses time olcelim.

  for (int x = 0 ; x < 14345 ; x ++)  {
    for (int y = 0 ; y < 15345 ; y ++)  {
      for (int z = 0 ; z < 12345 ; z ++)  {
        int testval = min(z, 12345);
        int sonuc = testval << 2;

  Serial.print("Process Time = ");
  Serial.println(millis() - pv_time);

  while (1);


output is:
05:56:01.352 -> Start ...
05:56:01.352 -> 0
05:56:01.352 -> Process Time = 0
05:56:01.352 -> 0

Hi everyone ! sorry I am newbe. My problem is, I wonder, why for loop doesnt take time ?
What's wrong ? Can any expert show me the way what should I do ? thanks.

I’d suggest the optimiser sees that you don’t do anything with the values in your loops, and discards all the code using them.

if you actually used sonuc for something, it may kick along a bit slower??

1 Like

You could also benefit from reading this: How to get the best out of this forum - Using Arduino / Installation & Troubleshooting - Arduino Forum

and format your code properly.

besides your for loops doing nothing and the optimizer discarding them, this

does not do anything useful other than always assign the value z to testval.

How can I close the optimizer optimise flags ? Where is this ?

Why would you want to do that?

Try running it again without the while(1);

I suspect the initial values are so low because they are < 0.

Since you have that while loop there the program blocks before the loop() can execute subsequent times.

Running it without the while() loop results in the values increasing over time:

(This output does not account for the initial values, before the serial monitor can catch up.)
Here are some later values:

Running it without the while() loop results in the values increasing over time

Does it surprise you that the output of millis() increases over time?

yes I 've tried just now. I got same thing. it comes from printing process. But I wonder why For loop doesn't take time. no matter what is X, Y , Z or how many for loops inside one another inside.
For example lets make more loop. alfa , beta, gamma. etxc. it takes a littlebit time.
but it must be hours long or day long. why. I don't understand.

I gaveup. :slight_smile: Thank you all interested Friends. Im gonna find away.

Of course not; millis() is milliseconds since boot. It should only ever increase. I meant the Process Time values increase over time, not the millis() value;

What I'm saying though is that if you run it more than once it does eventually start taking 1 or 2 milliseconds per loop().

Actually, that empty while loop causes the program to crash when run on an ESP-12E. I suspect there is something about it that the arduino does not like. It is affecting the stack in some way perhaps- causing a crash on the ESP and causing the variable to become corrupted on the Uno...

Can you explain this?

Arduino Uno:


Yes. You understood me. Thats what I mean. Somethings are doesnt make sense.
If we do the same thing with VB or C# on the computer I am sure that loops will take time. but C compiler on arduino doesn't. MCU can do that but compiler can not. BTW thankyou for your interests. Regards. bye.

It does something similar in Java. If you include the empty while loop it will block before the thread actually gets to the last print statement.



I suspect this has something to do with the scheduler; once the main thread gets stuck in the infinite loop, it is effectively frozen, and can no longer do anything. However since the order of operations in the C code do not directly correlate to the order of operations in the machine code, things can appear to happen out of order. So that's why we get a 0 for the millis() value on the Uno.

However if you remove the while(), we do start seeing 1's and 2's eventually for the process time, which leads me to believe the for loops are running on the order of micro seconds when we are seeing 0's.

However doing the same on the Uno still results in the same output as before, due to the different behavior from the while loop causing it to freeze.

Ok problem solved. Just change millis() to micros() and remove the while loop.



But I agree they do run much faster on here than they do in Java. Does anyone know why?

I solved .
Found some tips at "Optimization flags arduino 1.6.5"
I made change on the platform.txt.

line 28:
compiler.cpp.flags=-c -g -Os {compiler.warning_flags} -std=gnu++11 -fpermissive ..........
compiler.cpp.flags=-c -g -O0 {compiler.warning_flags} -std=gnu++11 -fpermissive ...........
( no optimization flag.) then my sketch produce output as I expected.

void loop() {
  // put your main code here, to run repeatedly:

  // * * * * * *
  pv_time  = millis(); // proses time olcelim.

  for (int x = 0 ; x < 14 ; x ++)  {
    for (int y = 0 ; y < 153 ; y ++)  {
      for (int z = 0 ; z < 123 ; z ++)  {
         testval = min(z, 123);
         sonuc = testval << 2;

int target = testval;
  Serial.print("Process Time = ");
  Serial.println(millis() - pv_time);

 while (1);


then output is :

09:07:41.727 -> Start ...
09:07:41.727 -> 0 ( waits little bit , for loops running as expected )
09:07:42.476 -> Process Time = 716
09:07:42.476 -> 717

good. Thank you all guided me.

so many thank for your effort friend. regards. bye.

On the ESP processors the loop function needs to complete, to do a bit of house keeping with the operating system that you need in order to use this chip. This OS is normally hidden but preventing the loop function from completing will expose its ugly head.

Awesome. Thank you. Do you happen to know what that feature is called? If I wanted to read more about it?

Have a look at


Interestingly I also changed my optimisation from -Os to -O3 and now for some reason I am able to see the entire trace starting at time = 0.

(Two separate runs)

I would still recommend using micros() in this situation.

The whole issue stemmed from the fact that millis() was preventing you from seeing the actual runtime of the first few iterations of the for loop. De-optimizing may allow you to slow down those loops to sub-millisecond times, but would one really want to do that in any practical application?

But so glad you got it figured out!