Can I use a Duff Device on an array in C?

I have a loop here and I want it to work faster. I turn into a large array. Recently I heard about the Duff Device, can it be applied to this loop? any ideas?

for (i = 0; i < dim; i++) {
    for (j = 0; j < dim; j++) {
        dst[RIDX(dim-1-j, i, dim)] = src[RIDX(i, j, dim)];
    }
}
+5
source share
13 answers

Please do not use the Duff device. Thousands of programmers will help you with maintenance. I used to work for a training company, where someone found it funny to present the device on the first ten pages of their C programming course. It was impossible to cope as an instructor, unless (because, apparently, the guy who wrote this piece of the course, you believe into the encoding "kewl").

, , .

+19

, ?

?

, , , , ?

, : , Duff Device , .

. . , , . ( - , , , , - , . .)

, Duff Device , , , .

+5

. , . , , , , .

, , , , RIDX , dst , . , , .

+3

Duff Device - a . , Duff Device.

+2

, .

- . , , , , ( - ).

, , - , -, , .

?

, .

: , , , , . , , , .

, , , , , , , .

+2

dst[RIDX(dim-1-j, i, dim)] = src[RIDX(i, j, dim)];

, .

+2

, Duff Device, , RIDX(). , , - ... , , .

+1

, , dim 2 . - . 5 memCopy(). :)

+1

, . Duff Device ( , ).

- Duff Device , . , , . , , .

0

, , , , , . , .

, Duff, , , , , ( ), ! , , , . , Duff , , , .

, , , ( ), , , .

0

Duff .

, , . :

  if (bit == 1)
  {
     write to the set port.
  }
  else
  {
     write to the clear port.
  }
  write high clock bit.
  write low clock bit.

Duff, .

, nibble ( - 4 ). switch nibble. 4 - if, ().

, Duff ; .

0

, , Duff . , , . , Duff - , .

0

, , , , , .. , , .

. , malloc()/free(), , - . , malloc, . , malloc 32- . 160% , , .

Duff Device , , . , .

I expect that the lower the level of optimization, the more likely future optimizations will be code breaking. I see that my habit of discarding line channels in code designed to run on multiple platforms and inserting line feeds into the print and write functions on each platform will run into problems in several things discussed in this thread.

-gcouger

0
source

All Articles