Right guys this is a trick which you either know or dont know.
It is the freakin tits.
I started using this technique when I wrote a GLIB for work.
It has to be cross platform so I couldnt take advantage of any architecture specific niceness.
Anyway in my benchmark tests it trounced memcpy for filling memory and also works on more complicated matters.
The technique is called Duffs Device. It uses case fallthrough to very quickly iterate through a list basically unrolling it.
Its in a macro so cant really be used for beefy loops but for tight ones its amazing.
#define DUFF_DEVICE_8(aCount, aAction) \
{ \
int count_ = (aCount); \
int times_ = (count_ + 7) >> 3; \
switch (count_ & 7){ \
case 0: do { aAction; \
case 7: aAction; \
case 6: aAction; \
case 5: aAction; \
case 4: aAction; \
case 3: aAction; \
case 2: aAction; \
case 1: aAction; \
} while (--times_ > 0); \
} \
}
If you know what loop unrolling is then youll know how to use it, if not reply and ill try answer any questions.