Arduino Mega 2560 slower than uno

I have an Arduino Mega and uno, both connected to an lcd display. The Mega filled the screen with the same code noticeable slower.
So i made 2 tests:

  1. Calculation
long t;
long x;
void setup()
{
  Serial.begin(9600);
}
void loop()
{
  t = millis();
  for(unsigned int a = 0; a <= 10000; a++)
  {
    if(a % 2 == 1)
    {
      x = x*2;
    }
    else
    {
      x = x/2;
    }
  }
  Serial.println(millis()- t);
}

Result
both took 202-204ms

  1. Write ports
long t;
long x;
void setup()
{
  Serial.begin(9600);
}
void loop()
{
  t = millis();
  for(unsigned int a = 0; a <= 10000; a++)
  {
    if(a % 2 == 1)
    {
      digitalWrite(11,1);
    }
    else
    {
      digitalWrite(11,0);
    }
  }
  Serial.println(millis()- t);
}

Result
Mega: 72-73ms
uno: 54-55ms
Both use an PWM Pin

Anybody here with similar results?
So the Mega is slower at Writing on the ports because he has more I/O?Can someone explain it to me? Im really dissapointed :.

digitalWrite() is slow because it has to convert the pin number you give to a port number and a bit within that port. So maybe the process takes longer on a Mega because there are more ports to deal with.

I wonder if the difference is the same for every pin or are some of them easier to "decode" than others.

You can always use direct port manipulation to avoid the overhead of digitalWrite() and digitalRead() but your code will be a bit more complicated.

...R