tags:

views:

283

answers:

4

Hi,

I once saw a programming pattern (not design), how to implement a fast copy of buffers. It included an interleaved loop and switch. The thing was, it copied 4 bytes most of the time, only the last few bytes of the buffer were copied using smaller datatypes.

Can someone tell me the name of it? It's named after a person. It's done in C and the compiler output is nearly optimal.

+9  A: 

Use memcpy(), it's standard, portable and in many cases optimized well too.

Michał Górny
you are right - that's the thing to use. I just wanted to know the pattern (though not using it).
Tobias Langner
+1. memcpy will usually be much (read 2x to 4x) faster.
Eric Bainville
Duff's device is optimised to reduce the cost of testing the loop conditional, but in its most common form copies a byte at a time. memcpy is typically optimised to the word size of the architecture, possibly also including some unrolling to reduce the number of tests, and only copying bytes at either end of unaligned buffers
Pete Kirkham
memcpy may also make use of other tricks, using SIMD instructions, or even DMA transfers.
jalf
+10  A: 

It sounds like you're thinking of Duff's device.

Laurence Gonsalves
You beat me by 3 seconds :)
qrdl
you were the fastest - thank you.
Tobias Langner
+2  A: 

Duff's device

qrdl
thank you, that's what I searched
Tobias Langner
+1  A: 

It is called Duff's device, see on Wikipedia

If you want to implement / utilize a fast copy, then first look at your compiler's implementation; it might use a lot more sophisticated algorithm using advanced features of your CPU. The Intel compilers have pretty sophisticated versions for example.

LaszloG