user88151: I have this code
#include <Arduino.h>




void setup() {
  
  DDRD  = B00001000;

}
void delayNanoSeconds(int time){
    int ans = (float)time / 62.5;
    for(int i = 0; i < ans; i++){
        _NOP();


    }


}
#define delay250 _NOP(); _NOP();_NOP(); _NOP();
#define delay1000 delay250 delay250 delay250 delay250
void loop() {
  while(1){
       PORTD ^= B00001000;
       delay1000; delay1000;
  }                
 
  }

but I am getting delay of 2300 us
here is a picture of the logic analyzer 
and when I change delay to 250 or 1000 I am still getting this extra 300ns so why

user7508: Remember: every single thing the CPU does takes time. Here is a
disassembly of your loop():
    ldi  r25, 0b00001000 ; r25 = B00001000
1:  in   r24, PORTD      ; r24 = PORTD
    eor  r24, r25        ; r24 ^= r25
    out  PORTD, r24      ; PORTD = r24
    nop
    nop
    ...
    nop
    rjmp 1b              ; goto previous label 1

As you see, besides your series of nop, there are four instructions in
the loop:

in, eor and out take one cycle each
rjmp takes two cycles.

That is a total overhead of 5 CPU cycles, or 312.5 ns when
running at 16 MHz. Pretty close to what you are seeing.
If you want an exact timings, you will have to disassemble your code and
count the overhead cycles, as I did here. Then, I suggest this simpler
approach:
#define OVERHEAD_CYCLES 5

void loop() {
    for (;;) {
        PORTD ^= _BV(PD3);
        _delay_us(2 - OVERHEAD_CYCLES / (F_CPU / 1e6));
    }
}


88151: <p>I have this code</p>
<pre><code>#include &lt;Arduino.h&gt;




void setup() {
  
  DDRD  = B00001000;

}
void delayNanoSeconds(int time){
    int ans = (float)time / 62.5;
    for(int i = 0; i &lt; ans; i++){
        _NOP();


    }


}
#define delay250 _NOP(); _NOP();_NOP(); _NOP();
#define delay1000 delay250 delay250 delay250 delay250
void loop() {
  while(1){
       PORTD ^= B00001000;
       delay1000; delay1000;
  }                
 
  }
</code></pre>
<p>but I am getting delay of 2300 us
here is a picture of the logic analyzer <a href="https://i.stack.imgur.com/dYRqg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dYRqg.png" alt="enter image description here" /></a>
and when I change delay to 250 or 1000 I am still getting this extra 300ns so why</p>
 7508: <p>Remember: every single thing the CPU does takes time. Here is a
disassembly of your <code>loop()</code>:</p>
<pre class="lang-lisp prettyprint-override"><code>    ldi  r25, 0b00001000 ; r25 = B00001000
1:  in   r24, PORTD      ; r24 = PORTD
    eor  r24, r25        ; r24 ^= r25
    out  PORTD, r24      ; PORTD = r24
    nop
    nop
    ...
    nop
    rjmp 1b              ; goto previous label 1
</code></pre>
<p>As you see, besides your series of <code>nop</code>, there are four instructions in
the loop:</p>
<ul>
<li><code>in</code>, <code>eor</code> and <code>out</code> take one cycle each</li>
<li><code>rjmp</code> takes two cycles.</li>
</ul>
<p>That is a total overhead of 5 CPU cycles, or 312.5 ns when
running at 16 MHz. Pretty close to what you are seeing.</p>
<p>If you want an exact timings, you will have to disassemble your code and
count the overhead cycles, as I did here. Then, I suggest this simpler
approach:</p>
<pre class="lang-cpp prettyprint-override"><code>#define OVERHEAD_CYCLES 5

void loop() {
    for (;;) {
        PORTD ^= _BV(PD3);
        _delay_us(2 - OVERHEAD_CYCLES / (F_CPU / 1e6));
    }
}
</code></pre>
