Time sensitive code, Trying to speed up

Trying to convert each bit of a double into byte, i know the code below works

I have these 2 for loops is there anyway to speed them up, using more bit-shifting?

    int array_Position = 0;
    byte array[x];
    
    union 
    {
      double variable;
      byte double_Array[8];
     } Union;


      for (int i = 7; i >= 0; i--)
      {
        for (int j = 6; j >= 0; j-=2)
        {          
          array[array_Position] = ((Union.double_Array[i] >> j) & 0x03);
          array_Position++;
        }
     }

First thing you can do is not use doubles, since for AVR they are the same size (4 bytes/32-bits) as floats.

Regards, Ray L.

Unroll the loop. Since the number of iterations of i and j is known at compile time, just write out 32 assignments.

If the iterations aren't known at compile time but are guaranteed elsewhere to be less than 256, use bytes as your loop counters - an int takes two operations to do any maths or comparison because it's two bytes.

Put the definitions of i and j in setup() so as not to keep redefining them in loop().

for (int i = 7; i >= 0; i--) { for (int j = 6; j >= 0; j-=2) { array[array_Position] = ((Union.double_Array >> j) & 0x03); * array_Position++;* * }* * }* [/quote] Shifting the same byte six places, and then shifting it 4, and then 2, is not very efficient. And on the arduino, shifting a byte 6 places requires six instruction cycles, so not fast, either. It would seem to me, to be a better idea to read the 2 least significant bits first, and then shift the byte 2 places, and read the 2 least significant bits again, and then repeat this a total of four times to get all of the bit-pairs in the byte.

Put the definitions of i and j Before setup() so as not to keep redefining them in loop().

Cross/Henry, how does that take up processor cycles? It just advances the stack pointer by a larger value, no extra cycles at all.

It may be relevant if we knew how often this gets called inside one iteration of loop(). I would guess that this activity is done many times for each loop().

Using local variables may be a hint to the compiler that it can keep them in registers, more so that if they are in global variables.

This piece of code is part of much larger library, I am sending the Arduino Udp messages at 5ms intervals and need the processing to be done within that time. I need to use a double.

I have made the "for loop" iteration defined outside the main loop.

The suggestion of changing the shifting is a possibility, but i need the bit pairs in a certain order, so i would need to shift them back of change the position of the array they go into, with that in mind would it still be quicker? I also read somewhere that going down in a for loop was marginally quicker.

Any other suggestions? Using while loops would slow it down right?

Thanks all for your help!

I am sending the Arduino Udp messages at 5ms intervals and need the processing to be done within that time. I need to use a double.

A variable defined as "double" on an Arduino takes up four bytes, so what is the point of the union? You can't use it to refer to an 8-byte double coming from elsewhere.