Slow shifting out

I am shifting out 288 bits into a TLC5947 pwm driver. There are 12 bits of resolution (24 outputs x 12 bits = 288). 12 bits so 4096 possibilities. To test it out I am shifting out 288 bits 4096 times, code shown below:

int latchPin = 7;
int clockPin = 5;
int dataPin = 6;
void setup() {
  pinMode(latchPin, OUTPUT);
   pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}
void loop() 
{
  long a;
  for(a = 0; a < 4096; a++)
  {
    digitalWrite(latchPin,LOW);
    shiftOut(a); 
    digitalWrite(latchPin,HIGH);  
  }
}



// the heart of the program
void shiftOut(long myDataOut) 
{
  int pinState;
  digitalWrite(dataPin, 0);
  digitalWrite(clockPin, 0);
  int outPut;
  for (outPut=0; outPut < 24; outPut++)  //24 different outputs
  {
    for(int val = 11; val >= 0; val--)    //12 bit number
    {
      digitalWrite(clockPin, 0);
      if ( myDataOut & (1<<val) ) 
      {
        pinState = 1;
      }
      else 
      {      
        pinState= 0;
      }

      digitalWrite(dataPin, pinState);
      digitalWrite(clockPin, 1);
      digitalWrite(dataPin, 0);
    }
  }
  //stop shifting
  digitalWrite(clockPin, 0);
}

However, going through the entire for loop takes roughly 25 seconds! Is there something particular that is causing this slowness or is simply turning all those pins high and low that many times slowing everything down. Since this is a fairly simple code I am thinking of switching over to true-c with AVR studio 4, with faster pin switching.

So your code is:

Call shiftOut 4096 times. Inside of shiftOut, do 1/0 loop 11 times, 24 times.

So 11 * 24 * 4096 = 1,081,344 loops. It doesn't take long long to see where the 25 seconds comes from.

Any reason you are not used the pre-made TLC549x library?

I decided not to use the library because the 5947 is much simpler, only shift 288 bits in. I found out that the digitalWrite function takes a lot of time to execute so I changed all the digitalWrites to the “true c” style and now it works great.

int latchPin = 7;
int clockPin = 5;
int dataPin = 6;
void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
}
void loop() 
{
  long a;
  for(a = 0; a < 4096; a++)
  {
    PORTD &= ~(1<<7);//digitalWrite(latchPin,LOW);
    shiftOut(a); 
    PORTD |= (1<<7);//digitalWrite(latchPin,HIGH);  
    //delayMicroseconds(600);
  }
}



// the heart of the program
void shiftOut(long myDataOut) 
{
  int pinState;
  //PORTD &= ~(1<<6);//digitalWrite(dataPin, 0);
  //PORTD &= ~(1<<5);//digitalWrite(clockPin, 0);
  int outPut;
  for (outPut=24; outPut >= 0; outPut--)  //24 different outputs
  {
    for(int val = 11; val >= 0; val--)    //12 bit number
    {
      PORTD &= ~(1<<5);//digitalWrite(clockPin, 0);
      if ( myDataOut & (1<<val) ) 
      {
        pinState = 1;
      }
      else 
      {      
        pinState= 0;
      }

      if(pinState == 0)
        PORTD &= ~(1<<6);//digitalWrite(dataPin, pinState);
      else
      {
        if(outPut == 0)
        PORTD |= (1<<6);
        else
        PORTD &= ~(1<<6);
      }
      PORTD |= (1<<5);//digitalWrite(clockPin, 1);
      PORTD &= ~(1<<6);//digitalWrite(dataPin, 0);
    }
  }
  //stop shifting
  PORTD &= ~(1<<5);//digitalWrite(clockPin, 0);
}

Hi Razorconcepts,
-edit-

As I was looking further I found your explanation http://www.instructables.com/id/Arduino-is-Slow-and-how-to-fix-it/step3/Exspearimintation/ of how
PORTB |= _BV(PB5);
PORTB &= ~_BV(PB5);
may have to change to
PORTB = 0b00100000;
PORTB = 0b00000000;
to work in versions of Arduino later than 15 (and yes thats the case for Arduino18)

So would in your example above:
PORTD &= ~(1<<7);
PORTD |= (1<<7);
and
PORTD &= ~(1<<5);
etc.
Have to change to what works in Arduino15+, or is that a different issue??