Duff's device( 达夫设备)

Tom Duff曾经写过这样的函数,其中使用了奇怪的switch...case,他自己称之为duff's device。猛一看,似乎不合乎C的语法规则。但实际上能在所有C编译器下编译通过。其效率据说可以大大提高。

Anoop写了一个程序进行测试。转贴如下:

  1. /* The Duff device  
  2.  * 
  3.  * An infamous example of how a compiler can accept code that should 
  4.  * be illegal as per the language definition. To add insult to injury, 
  5.  * the illegal code actually runs faster.  
  6.  * 
  7.  * The functions send and send2 accomplish the same goal (copying a 
  8.  * string from one location to another) but send2 manages to screw 
  9.  * with your head and achieve its goal much faster (on most 
  10.  * architectures).  
  11.  *  
  12.  * The answer to the puzzle of how send2 actually works is exposed in 
  13.  * the function send3 (see the comment above the function send3). 
  14.  * 
  15.  * This strange piece of code is named after the programmer who 
  16.  * discovered this 'optimization' technique. 
  17.  * 
  18.  * -- Anoop Sarkar <anoop at cs.sfu.ca> 
  19.  **/  
  20.   
  21. #include <stdio.h>  
  22. #include <stdlib.h>  
  23. #include <string.h>  
  24. #include <sys/time.h>  
  25.   
  26. /* pick BUFLEN to be a suitably large number to show the speed 
  27.      difference between send and send2 */  
  28. const size_t BUFLEN = 100000000;  
  29.   
  30. void  
  31. send (register char *to, register char *from, register int count)  
  32. {  
  33.     do  
  34.       *to++ = *from++;  
  35.     while(--count>0);  
  36. }  
  37.   
  38. void  
  39. send2 (register char *to, register char *from, register int count)  
  40. {  
  41.     register int n = (count+7)/8;  
  42.     switch (count % 8) {  
  43.     case 0: do {       *to++ = *from++;  
  44.     case 7:            *to++ = *from++;  
  45.     case 6:            *to++ = *from++;  
  46.     case 5:            *to++ = *from++;  
  47.     case 4:            *to++ = *from++;  
  48.     case 3:            *to++ = *from++;  
  49.     case 2:            *to++ = *from++;  
  50.     case 1:            *to++ = *from++;  
  51.     } while(--n>0);  
  52.     }  
  53. }  
  54.   
  55. /* The answer to the mystery turns out to be simple loop unfolding. 
  56.  * send2 uses the semantics for switch statements in C to provide a 
  57.  * mnemonic for how many assignments should occur within the body of 
  58.  * the do-while loop.  
  59.  * 
  60.  * So why is send2 faster than send on some architectures? The 
  61.  * conditional is a slow instruction to execute on many machine 
  62.  * architectures.  
  63.  * 
  64.  * Try compiling with gcc with and without the -O3 flag. Turning the 
  65.  * optimizer on (using -O3) shows the power of code optimization: send 
  66.  * runs as fast as send2 with the optimizer on. 
  67.  **/   
  68.   
  69. int main (int argc, char **argv)  
  70. {  
  71.     char *from, *to;  
  72.     int i;  
  73.     struct timeval before, after;  
  74.   
  75.     from = (char *) malloc(BUFLEN * sizeof(char));  
  76.     to = (char *) malloc(BUFLEN * sizeof(char));  
  77.   
  78.     memset(from, 'a', (BUFLEN * sizeof(char)));  
  79.     printf("array init done/n");  
  80.   
  81.     printf("calling send/n");  
  82.     gettimeofday(&before, NULL);  
  83.     send(to, from, BUFLEN);  
  84.     gettimeofday(&after, NULL);  
  85.     printf("secs=%d/n", after.tv_sec - before.tv_sec);  
  86.   
  87.     printf("calling send2/n");  
  88.     gettimeofday(&before, NULL);  
  89.     send2(to, from, BUFLEN);  
  90.     gettimeofday(&after, NULL);  
  91.     printf("secs=%d/n", after.tv_sec - before.tv_sec);  
  92.   
  93.     if (strcmp(from,to) == 0) {  
  94.       printf("from=to/n");  
  95.     } else {  
  96.       printf("from!=to/n");  
  97.     }  
  98.   
  99.     free(from);  
  100.     free(to);  
  101.   
  102.     return(0);  
  103. }  

运行结果:
[redbull@calabash tmp]$gcc -O3 duff.c
[redbull@calabash tmp]$ ./a.out
array init done
calling send
secs=4
calling send2
secs=1
from=to

posted @ 2011-07-19 16:23  wanmero  阅读(380)  评论(0编辑  收藏  举报