user37539: Hi I am using Arduino Nano to gamify my experience on Unity3D. I know that Update() in Unity3D runs every frame per second but I because I'm not an electronics guy I don't know how loop() function runs on Arduino.
Does it depends on number of CPU cycles(or micro processor chip in case of Arduino) or what? Please explain. I facing some lags in my game. I can fix them only if know how is loop() function running on Arduino
There ain't a lot of information about loop() in arduino documentation

user30005: It depends on what's in the loop. From a few clock cycles to infinite hang.

user37632: try looking up arduino functions
micros()
and
delayMicroseconds()
If you might need your loop to wait longer than 16000 microseconds then also look up the other delay() function which does milliseconds.
By the way, the internal clock is very slightly off and varies with room temperature, so don't rely on it staying synchronised to your other device without a handshake.

user42373: A look at main.cpp (for avr code in my installation) in file:
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino\main.cpp

shows that the Arduino runs loop() alternately with serialEventRun() in an infinite loop:  
int main(void)
{
    init();
    initVariant();
 #if defined(USBCON)
    USBDevice.attach();
#endif

    setup();

    for (;;) {
        loop();
        if (serialEventRun) serialEventRun();
    }

    return 0;
}

Inspection of HardwareSerial.cpp shows that serialEventRun() simply calls serialEvent(), if you have defined it in your sketch. 
This means that you can write your own infinite loop within loop() perfectly safely, unless you have written serialEvent() code and are expecting it to run regularly.
This begs the question: if serialEvent() is called sequentially with loop(), will serialEvent() be called if loop() never returns? In other words, is serialEvent() interrupt-driven as well as being called when loop() returns?  A quick test -see below- shows that it is not interrupt-driven, so the previous paragraph is true.
/*
  Serial Event Checker

  A sketch to see if serialEvent() is interrupt-driven.
  Will serialEvent() be called if loop() never returns?

  This code is based on Tom Igoe's SerialEvent example.

  On (at least) Arduino Uno we find that serialEvent() simply runs
  sequentially with loop().  When loop() never returns serialEvent()
  is never called.

  NOTE: The serialEvent() feature is not available on the Leonardo, Micro, or
  other ATmega32U4 based boards.

  R Symonds-Tayler 2018-02-01

  This example code is in the public domain.

*/

String inputString = "";         // a String to hold incoming data

void setup() {
  // initialize serial:
  Serial.begin(115200);
  // reserve 200 bytes for the inputString:
  inputString.reserve(200);
}

// comment out this definition to allow loop() to return
#define INFINITE_LOOP

void loop() { 
#ifdef INFINITE_LOOP
    while(1);
#endif

}

/*
  SerialEvent occurs whenever a new data comes in the hardware serial RX. This
  routine is run between each time loop() runs, so using delay inside loop can
  delay response. Multiple bytes of data may be available.
*/
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag so the main loop can
    // do something about it:
    if (inChar == '\n') {
      Serial.print("Got: '");
      Serial.print(inputString);
      Serial.println("'");
      // clear the string:
      inputString = "";
    }
  }
}

This means that serialEvent() code might just as well go into your main loop() within a:
if(serial.available()){
   // your code here...
}

block.

user359: the instructions between the end of loop() and the next run of it are a ret, jmp and a call. 
You can consider the main function to be effectively:
void main(){

    init();
    while(1) loop();

}

Though in reality there is some setup done before init() to make millis() and such work.

user4143: Loop runs as long as loop needs to run. 
Instructions in a CPU run sequentially. The more instructions there are the longer it takes to run.
The more code you put in loop the longer loop will run. 
There are two ways to know how long each iteration of loop will take:

Profiling: Actively time each iteration of loop, though be warned, the act of timing will affect the amount of time taken.
Cycle counting. Compile the source into assembly and total up the number of clock cycles needed for all the instructions (including calls to functions). Arduous for small loops, a gargantuan task for anything else.

Also note that many external factors can affect how long loop() takes to run - such as serial communication speed, etc.

37539: <p>Hi I am using <code>Arduino Nano</code> to gamify my experience on Unity3D. I know that <code>Update()</code> in Unity3D runs every frame per second but I because I'm not an electronics guy I don't know how <code>loop()</code> function runs on Arduino.</p>

<p>Does it depends on number of CPU cycles(or micro processor chip in case of Arduino) or what? Please explain. I facing some lags in my game. I can fix them only if know how is loop() function running on Arduino</p>

<p>There ain't a lot of information about <code>loop()</code> in arduino <a href="https://www.arduino.cc/en/Reference/Loop" rel="noreferrer">documentation</a></p>
 30005: <p>It depends on what's in the loop. From a few clock cycles to infinite hang.</p>
 37632: <p>try looking up arduino functions
micros()
and
delayMicroseconds()
If you might need your loop to wait longer than 16000 microseconds then also look up the other delay() function which does milliseconds.</p>

<p>By the way, the internal clock is very slightly off and varies with room temperature, so don't rely on it staying synchronised to your other device without a handshake.</p>
 42373: <p>A look at main.cpp (for avr code in my installation) in file:</p>

<pre><code>C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino\main.cpp
</code></pre>

<p>shows that the Arduino runs loop() alternately with serialEventRun() in an infinite loop:  </p>

<pre><code>int main(void)
{
    init();
    initVariant();
 #if defined(USBCON)
    USBDevice.attach();
#endif

    setup();

    for (;;) {
        loop();
        if (serialEventRun) serialEventRun();
    }

    return 0;
}
</code></pre>

<p>Inspection of HardwareSerial.cpp shows that serialEventRun() simply calls serialEvent(), if you have defined it in your sketch. </p>

<p>This means that you can write your own infinite loop within loop() perfectly safely, unless you have written serialEvent() code and are expecting it to run regularly.</p>

<p>This begs the question: if serialEvent() is called sequentially with loop(), will serialEvent() be called if loop() never returns? In other words, is serialEvent() interrupt-driven as well as being called when loop() returns?  A quick test -see below- shows that it is <strong>not</strong> interrupt-driven, so the previous paragraph is true.</p>

<pre><code>/*
  Serial Event Checker

  A sketch to see if serialEvent() is interrupt-driven.
  Will serialEvent() be called if loop() never returns?

  This code is based on Tom Igoe's SerialEvent example.

  On (at least) Arduino Uno we find that serialEvent() simply runs
  sequentially with loop().  When loop() never returns serialEvent()
  is never called.

  NOTE: The serialEvent() feature is not available on the Leonardo, Micro, or
  other ATmega32U4 based boards.

  R Symonds-Tayler 2018-02-01

  This example code is in the public domain.

*/

String inputString = "";         // a String to hold incoming data

void setup() {
  // initialize serial:
  Serial.begin(115200);
  // reserve 200 bytes for the inputString:
  inputString.reserve(200);
}

// comment out this definition to allow loop() to return
#define INFINITE_LOOP

void loop() { 
#ifdef INFINITE_LOOP
    while(1);
#endif

}

/*
  SerialEvent occurs whenever a new data comes in the hardware serial RX. This
  routine is run between each time loop() runs, so using delay inside loop can
  delay response. Multiple bytes of data may be available.
*/
void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag so the main loop can
    // do something about it:
    if (inChar == '\n') {
      Serial.print("Got: '");
      Serial.print(inputString);
      Serial.println("'");
      // clear the string:
      inputString = "";
    }
  }
}
</code></pre>

<p>This means that serialEvent() code might just as well go into your main loop() within a:</p>

<pre><code>if(serial.available()){
   // your code here...
}
</code></pre>

<p>block.</p>
 359: <p>the instructions between the end of loop() and the next run of it are a ret, jmp and a call. </p>

<p>You can consider the main function to be effectively:</p>

<pre><code>void main(){

    init();
    while(1) loop();

}
</code></pre>

<p>Though in reality there is some setup done before <code>init()</code> to make <code>millis()</code> and such work.</p>
 4143: <p>Loop runs as long as loop needs to run. </p>

<p>Instructions in a CPU run sequentially. The more instructions there are the longer it takes to run.</p>

<p>The more code you put in loop the longer loop will run. </p>

<p>There are two ways to know how long each iteration of loop will take:</p>

<ol>
<li>Profiling: Actively time each iteration of loop, though be warned, the act of timing will affect the amount of time taken.</li>
<li>Cycle counting. Compile the source into assembly and total up the number of clock cycles needed for all the instructions (including calls to functions). Arduous for small loops, a gargantuan task for anything else.</li>
</ol>

<p>Also note that many external factors can affect how long loop() takes to run - such as serial communication speed, etc.</p>
