user19961: I want to produce quick square waveform for testing purposes. If I will use this code:
void setup()
{
 pinMode(2, OUTPUT);
}

void loop() 
{
 bitSet(PORTD, 2);
 bitClear(PORTD, 2);
} 

I will get expected pulses but I also will get these 6.25 us dips in waveform roughly every 1 ms.
I use latest Arduino 1.6.9 environment.
Whole loop cycle take 1 us which mean 16 instructions (62.5 ns per instruction). So I will get 125 ns high and rest of the time low signal.
Any idea what I'm doing wrong? 
Same results if I use asm sbi and sci instead of macros bitSet/bitClear.
Thank you!

/*
So after yours advices and little bit further searching I came up with solution to produce clean square waveform. It make 2.66 Mhz and it's probably fastest what you can get:
void setup()
{
 pinMode(2, OUTPUT);
 noInterrupts();   
}

void loop() 
{
 while(true)
 {
 bitSet(PORTD, 2);
 bitClear(PORTD, 2);
 }
} 


user13432: 
I want to produce quick square waveform for testing purposes.
Whole loop cycle take 1 us which mean 16 instructions (62.5 ns per instruction). So I will get 125 ns high and rest of the time low signal.
  Any idea what I'm doing wrong? 

You are assuming that what goes on outside loop() and the call to loop() takes no time at all. 
This is what the Arduino core main() looks like. 
int main(void)
{
    init();

    initVariant();

#if defined(USBCON)
    USBDevice.attach();
#endif

    setup();

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

    return 0;
}

There is both the call to loop(), if-branch, and for-branch. This all adds up. On top of this there are a number of Interrupt Service Routines (ISR) such as for the Timer which is used for the micros() and millis() count. These will interleave periodically (as you have observed). 
Now the question is how to fix this and I think @Edgar Bonet has done an excellent job with that. 
Cheers!

user19409: If you just want a rough square wave to test and you don't care about the frequency then try this:  
void setup()
{
   pinMode(2, OUTPUT);
}

void loop() 
{
   digitalWrite(2, true);
   delay(2);
   digitalWrite(2, false);
   delay(2);
} 


user7508: As I explained in my comment, the reason for this glitch is the Timer 0
overflow interrupt kicking in every 1024 µs. This interrupt is used
by the Arduino core library for timekeeping. It is the basis of
millis(), micros() and delay(). The interrupt is enabled by the
init() function in
wiring.c.
There are several approaches to avoid that:
Avoid the Arduino core initialization
This may be overkill, but since it seems you are already familiar with
directly accessing the IO ports of the ATmega chip, it may well suit
you. You can use the Arduino IDE without the Arduino core initialization
simply by defining your own main():
#include <util/delay.h>

int main(void)
{
    DDRD |= _BV(PD2);        // PD2 as output
    for (;;) {
        PORTD |= _BV(PD2);   // PD2 high
        PORTD &= ~_BV(PD2);  // PD2 low
        _delay_us(0.625);    // 10 CPU cycles, loop time = 16 cycles
    }
}

Notice the extra delay: without it, the loop would complete in 6 CPU
cycles (2 per port access, 2 more to loop back). Not doing the Arduino
core initialization means you have no timekeeping and no analogRead()
or analogWrite(). Other Arduino functions should work normally but
some, like Serial.begin() will bring their own interrupts.
Disable interrupts
As suggested by Majenko, this may be the simplest way to have a steady
waveform:
void setup()
{
    pinMode(2, OUTPUT);
    noInterrupts();
}

void loop()
{
    PORTD |= _BV(PD2);   // PD2 high
    PORTD &= ~_BV(PD2);  // PD2 low
}

Without interrupts, you have no timekeeping, no serial communications
and, obviously, no attachInterrupt(). Most other Arduino functions
should work.
Use a hardware timer
This allows you to generate the signal without relying on the CPU. You
can keep interrupts enabled and do other work in the program while the
waveform goes undisturbed. The simplest way to generate such a waveform
is with analogWrite(). The resulting frequency is quite low though,
like 0.5 to 1 kHz. If you want 1 MHz, you will have to
configure the timer the hard way. For example:
void setup()
{
    DDRD |= _BV(PD3);     // OC2B = PD3 as output
    OCR2A = 15;           // period = 16 clock cycles
    OCR2B = 1;            // high during 2 clock cycles
    TCCR2A = _BV(COM2B1)  // non-inverting PWM
           | _BV(WGM20)   // fast PWM, TOP = OCR2A
           | _BV(WGM21);  // ditto
    TCCR2B = _BV(WGM22)   // ditto
           | _BV(CS20);   // clock at f_CPU
}

void loop() { }

The output is on pin PD3 = digital 3. This configuration will disable
analogWrite() on pins 3 (PD3) and 11 (PB3).

19961: <p>I want to produce quick square waveform for testing purposes. If I will use this code:</p>

<pre><code>void setup()
{
 pinMode(2, OUTPUT);
}

void loop() 
{
 bitSet(PORTD, 2);
 bitClear(PORTD, 2);
} 
</code></pre>

<p>I will get expected pulses but I also will get these 6.25 us dips in waveform roughly every 1 ms.</p>

<p>I use latest Arduino 1.6.9 environment.</p>

<p>Whole loop cycle take 1 us which mean 16 instructions (62.5 ns per instruction). So I will get 125 ns high and rest of the time low signal.</p>

<p>Any idea what I'm doing wrong? </p>

<p>Same results if I use asm sbi and sci instead of macros bitSet/bitClear.</p>

<p>Thank you!</p>

<p><a href="https://i.stack.imgur.com/VFrm0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VFrm0.png" alt="Oscilloscope waveform - 62.5 us detail"></a></p>

<p>/*</p>

<p>So after yours advices and little bit further searching I came up with solution to produce clean square waveform. It make 2.66 Mhz and it's probably fastest what you can get:</p>

<pre><code>void setup()
{
 pinMode(2, OUTPUT);
 noInterrupts();   
}

void loop() 
{
 while(true)
 {
 bitSet(PORTD, 2);
 bitClear(PORTD, 2);
 }
} 
</code></pre>
 13432: <blockquote>
  <p>I want to produce quick square waveform for testing purposes.</p>
  
  <p>Whole loop cycle take 1 us which mean 16 instructions (62.5 ns per instruction). So I will get 125 ns high and rest of the time low signal.
  Any idea what I'm doing wrong? </p>
</blockquote>

<p>You are assuming that what goes on outside loop() and the call to loop() takes no time at all. </p>

<p>This is what the Arduino core <a href="https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/main.cpp#L33" rel="nofollow noreferrer">main()</a> looks like. </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>There is both the call to loop(), if-branch, and for-branch. This all adds up. On top of this there are a number of Interrupt Service Routines (ISR) such as for the Timer which is used for the micros() and millis() count. These will interleave periodically (as you have observed). </p>

<p>Now the question is how to fix this and I think @Edgar Bonet has done an excellent job with that. </p>

<p>Cheers!</p>
 19409: <p>If you just want a rough square wave to test and you don't care about the frequency then try this:  </p>

<pre><code>void setup()
{
   pinMode(2, OUTPUT);
}

void loop() 
{
   digitalWrite(2, true);
   delay(2);
   digitalWrite(2, false);
   delay(2);
} 
</code></pre>
 7508: <p>As I explained in my comment, the reason for this glitch is the Timer 0
overflow interrupt kicking in every 1024&nbsp;µs. This interrupt is used
by the Arduino core library for timekeeping. It is the basis of
<code>millis()</code>, <code>micros()</code> and <code>delay()</code>. The interrupt is <a href="https://github.com/arduino/Arduino/blob/master/hardware/arduino/avr/cores/arduino/wiring.c#L279" rel="nofollow">enabled by the
<code>init()</code> function in
wiring.c</a>.
There are several approaches to avoid that:</p>

<h2>Avoid the Arduino core initialization</h2>

<p>This may be overkill, but since it seems you are already familiar with
directly accessing the IO ports of the ATmega chip, it may well suit
you. You can use the Arduino IDE without the Arduino core initialization
simply by defining your own <code>main()</code>:</p>



<pre class="lang-cpp prettyprint-override"><code>#include &lt;util/delay.h&gt;

int main(void)
{
    DDRD |= _BV(PD2);        // PD2 as output
    for (;;) {
        PORTD |= _BV(PD2);   // PD2 high
        PORTD &amp;= ~_BV(PD2);  // PD2 low
        _delay_us(0.625);    // 10 CPU cycles, loop time = 16 cycles
    }
}
</code></pre>

<p>Notice the extra delay: without it, the loop would complete in 6 CPU
cycles (2 per port access, 2 more to loop back). Not doing the Arduino
core initialization means you have no timekeeping and no <code>analogRead()</code>
or <code>analogWrite()</code>. Other Arduino functions should work normally but
some, like <code>Serial.begin()</code> will bring their own interrupts.</p>

<h2>Disable interrupts</h2>

<p>As suggested by Majenko, this may be the simplest way to have a steady
waveform:</p>

<pre class="lang-cpp prettyprint-override"><code>void setup()
{
    pinMode(2, OUTPUT);
    noInterrupts();
}

void loop()
{
    PORTD |= _BV(PD2);   // PD2 high
    PORTD &amp;= ~_BV(PD2);  // PD2 low
}
</code></pre>

<p>Without interrupts, you have no timekeeping, no serial communications
and, obviously, no <code>attachInterrupt()</code>. Most other Arduino functions
should work.</p>

<h2>Use a hardware timer</h2>

<p>This allows you to generate the signal without relying on the CPU. You
can keep interrupts enabled and do other work in the program while the
waveform goes undisturbed. The simplest way to generate such a waveform
is with <code>analogWrite()</code>. The resulting frequency is quite low though,
like 0.5 to 1&nbsp;kHz. If you want 1&nbsp;MHz, you will have to
configure the timer the hard way. For example:</p>

<pre class="lang-cpp prettyprint-override"><code>void setup()
{
    DDRD |= _BV(PD3);     // OC2B = PD3 as output
    OCR2A = 15;           // period = 16 clock cycles
    OCR2B = 1;            // high during 2 clock cycles
    TCCR2A = _BV(COM2B1)  // non-inverting PWM
           | _BV(WGM20)   // fast PWM, TOP = OCR2A
           | _BV(WGM21);  // ditto
    TCCR2B = _BV(WGM22)   // ditto
           | _BV(CS20);   // clock at f_CPU
}

void loop() { }
</code></pre>

<p>The output is on pin PD3 = digital 3. This configuration will disable
<code>analogWrite()</code> on pins 3 (PD3) and 11 (PB3).</p>
