user82148: I have the following code which checks the latency of another board.
    int triggerPin = 13;
    int dataPin = 9;
    int ejectorPin = A0;
    unsigned long t_start = 0;
    unsigned long t_end = 0;
    unsigned long t_total = 0;
    int value = 0;
    
    void setup() {
      // put your setup code here, to run once:
      pinMode(triggerPin, OUTPUT);
      pinMode(dataPin, OUTPUT);
      pinMode(ejectorPin, INPUT);
      Serial.begin(9600);
    }
    
    void loop() {
      t_start = micros();
      while (value < 10 ) {
        digitalWrite(triggerPin, HIGH);
        digitalWrite(dataPin, HIGH);
        delay(5.0);
        digitalWrite(dataPin, LOW);
        digitalWrite(triggerPin, LOW);
        delay(1.0);
        value = analogRead(ejectorPin);
      }
      t_end = micros();
    
      t_total = (t_end - t_start);
      Serial.println(double(t_total) / 1000);

Note that I'm using analogRead() inside the while loop. With this method I get 18.3 msec. However, if I use the analogRead() in the while condition:
    void loop() {
      t_start = micros();
      while (analogRead() < 10 ) {
        .
        .
        .

then I get 30.2 msec. I thought assigning value of analogRead() to a variable would take longer but here it's the opposite.
Can someone please explain the reason behind this behaviour?

user10794: Both of your examples use a while loop, so the while loop is not the issue.
An analogue read, with the prescalers unchanged, takes 104 µs. So, what you are really measuring is your 6 ms delays, plus a slight amount extra for the other code.
Since your first while loop necessarily takes 6+ ms, then you will naturally get a result which is a multiple of 6 ms. This appears to be happening, so you are getting two loops (18 ms). This doesn't prove a huge amount.

You need to read up on the Nyquist frequency
To get accurate sampling results you need to sample at twice the rate at which things change. Your first results may merely be the results of aliasing. Your 6 ms delays may cause you to take one sample on one transition, and another sample (maybe) a couple of transitions later. Thus your results are meaningless.

82148: <p>I have the following code which checks the latency of another board.</p>
<pre class="lang-cpp prettyprint-override"><code>    int triggerPin = 13;
    int dataPin = 9;
    int ejectorPin = A0;
    unsigned long t_start = 0;
    unsigned long t_end = 0;
    unsigned long t_total = 0;
    int value = 0;
    
    void setup() {
      // put your setup code here, to run once:
      pinMode(triggerPin, OUTPUT);
      pinMode(dataPin, OUTPUT);
      pinMode(ejectorPin, INPUT);
      Serial.begin(9600);
    }
    
    void loop() {
      t_start = micros();
      while (value &lt; 10 ) {
        digitalWrite(triggerPin, HIGH);
        digitalWrite(dataPin, HIGH);
        delay(5.0);
        digitalWrite(dataPin, LOW);
        digitalWrite(triggerPin, LOW);
        delay(1.0);
        value = analogRead(ejectorPin);
      }
      t_end = micros();
    
      t_total = (t_end - t_start);
      Serial.println(double(t_total) / 1000);
</code></pre>
<p>Note that I'm using <code>analogRead()</code> inside the while loop. With this method I get <strong>18.3 msec</strong>. However, if I use the <code>analogRead()</code> in the while condition:</p>
<pre class="lang-cpp prettyprint-override"><code>    void loop() {
      t_start = micros();
      while (analogRead() &lt; 10 ) {
        .
        .
        .
</code></pre>
<p>then I get <strong>30.2 msec</strong>. I thought assigning value of <code>analogRead()</code> to a variable would take longer but here it's the opposite.</p>
<p><strong>Can someone please explain the reason behind this behaviour?</strong></p>
 10794: <p>Both of your examples use a while loop, so the while loop is not the issue.</p>
<p>An analogue read, with the prescalers unchanged, takes 104 µs. So, what you are really measuring is your 6 ms delays, plus a slight amount extra for the other code.</p>
<p>Since your first while loop necessarily takes 6+ ms, then you will naturally get a result which is a multiple of 6 ms. This appears to be happening, so you are getting two loops (18 ms). This doesn't prove a huge amount.</p>
<hr />
<p>You need to read up on the <a href="https://en.wikipedia.org/wiki/Nyquist_frequency" rel="nofollow noreferrer">Nyquist frequency</a></p>
<p>To get accurate sampling results you need to sample at twice the rate at which things change. Your first results may merely be the results of aliasing. Your 6 ms delays may cause you to take one sample on one transition, and another sample (maybe) a couple of transitions later. Thus your results are meaningless.</p>
