莊周夢(mèng)蝶

          生活、程序、未來
             :: 首頁(yè) ::  ::  :: 聚合  :: 管理

          今天長(zhǎng)見識(shí)了

          Posted on 2007-11-29 18:17 dennis 閱讀(1184) 評(píng)論(0)  編輯  收藏 所屬分類: linux & C
              先是讀《Programming in Lua》第9章講coroutine,然后去google coroutine,找到Simon Tatham寫的一篇coroutine in c,講怎么在C語言中實(shí)現(xiàn)coroutine,文中先ugly地基于棧實(shí)現(xiàn)了一個(gè):
          int function(void) {
              
          static int i, state = 0;
              
          switch (state) {
                  
          case 0goto LABEL0;
                  
          case 1goto LABEL1;
              }
              LABEL0: 
          /* start of function */
              
          for (i = 0; i < 10; i++) {
                  state 
          = 1/* so we will come back to LABEL1 */
                  
          return i;
                  LABEL1:; 
          /* resume control straight after the return */
              }
          }

          這個(gè)方法簡(jiǎn)單,但是相當(dāng)丑陋,你必須手工維護(hù)這些標(biāo)簽。然后提到了Duff's Device技巧:
          switch (count % 8) {
                  
          case 0:        do {  *to = *from++;
                  
          case 7:              *to = *from++;
                  
          case 6:              *to = *from++;
                  
          case 5:              *to = *from++;
                  
          case 4:              *to = *from++;
                  
          case 3:              *to = *from++;
                  
          case 2:              *to = *from++;
                  
          case 1:              *to = *from++;
                                 } 
          while ((count -= 8> 0);
              }

          這段代碼能編譯通過嗎?能的,不信你試試,這是一段用于拷貝數(shù)組的代碼,我們一般拷貝數(shù)組是這樣做的:
          send(to, from, count)
                  register 
          short *to, *from;
                  register count;
                  {
                          
          do
                                  
          *to = *from++;
                          
          while(--count>0);
                  }
          如果循環(huán)的中的操作足夠快,那么其實(shí)大部分時(shí)間都是浪費(fèi)在判斷循環(huán)條件上面的,而通過Duff's Device通過switch語句將要進(jìn)行的連續(xù)循環(huán)操作的次數(shù)進(jìn)行了預(yù)判(根據(jù)擦case語句的位置)然后依次執(zhí)行,而不必每次都去進(jìn) 行測(cè)試條件,從而加速循環(huán)。這個(gè)技巧怎么應(yīng)用于實(shí)現(xiàn)更優(yōu)雅的coroutine呢?看代碼

          int function(void) {
              
          static int i, state = 0;
              
          switch (state) {
                  
          case 0/* start of function */
                  
          for (i = 0; i < 10; i++) {
                      state 
          = 1/* so we will come back to "case 1" */
                      
          return i;
                      
          case 1:; /* resume control straight after the return */
                  }
              }
          }
          更好的方式是使用宏:
          #define crBegin static int state=0; switch(state) { case 0:
          #define crReturn(i,x) do { state=i; return x; case i:; } while (0)
          #define crFinish }
          int function(void) {
              
          static int i;
              crBegin;
              
          for (i = 0; i < 10; i++)
                  crReturn(
          1, i);
              crFinish;
          }



          主站蜘蛛池模板: 万荣县| 长顺县| 正阳县| 囊谦县| 灵武市| 玉树县| 克什克腾旗| 丽江市| 高青县| 信阳市| 宁都县| 襄樊市| 宁波市| 朝阳市| 平阴县| 甘南县| 延吉市| 盘锦市| 淳化县| 田林县| 余庆县| 佳木斯市| 大洼县| 遵义县| 繁昌县| 大城县| 洛宁县| 巴林右旗| 五峰| 固镇县| 阿克| 齐河县| 宽甸| 侯马市| 岳西县| 即墨市| 应城市| 绥江县| 沐川县| 东莞市| 巫溪县|