Tobias Langner
Tobias Langner

Reputation: 10808

Pattern for fast copy in C

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.

Upvotes: 3

Views: 2148

Answers (4)

LaszloG
LaszloG

Reputation: 719

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.

Upvotes: 2

qrdl
qrdl

Reputation: 34968

Duff's device

Upvotes: 2

Laurence Gonsalves
Laurence Gonsalves

Reputation: 143204

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

Upvotes: 11

Michał Górny
Michał Górny

Reputation: 19243

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

Upvotes: 12

Related Questions