<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    莊周夢蝶

    生活、程序、未來
       :: 首頁 ::  ::  :: 聚合  :: 管理

    今天長見識了

    Posted on 2007-11-29 18:17 dennis 閱讀(1185) 評論(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è)方法簡單,但是相當(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) 行測試條件,從而加速循環(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;
    }



    主站蜘蛛池模板: 亚洲狠狠成人综合网| 免费精品一区二区三区在线观看| 亚洲av综合av一区二区三区| 久久亚洲成a人片| 亚洲一区无码精品色| 成年人网站在线免费观看| 日本一区二区免费看| 亚美影视免费在线观看 | 一个人看的www免费高清| 国产91在线|亚洲| 午夜影视日本亚洲欧洲精品一区| 亚洲国产精品一区二区九九| 全免费一级毛片在线播放| 91精品免费国产高清在线| 精品成人免费自拍视频| 九九九精品视频免费| 亚洲AV永久无码精品一福利 | 中文字幕免费视频精品一| 国产亚洲男人的天堂在线观看 | 亚欧在线精品免费观看一区| 女同免费毛片在线播放| a高清免费毛片久久| 国产成人精品免费大全| 色多多A级毛片免费看| 国产午夜亚洲精品不卡| 亚洲中文无码亚洲人成影院| 亚洲ts人妖网站| 亚洲另类精品xxxx人妖| 亚洲美女视频免费| 亚洲精品免费在线| 亚洲综合色丁香麻豆| 亚洲视频在线不卡| 亚洲国产精品网站久久| 亚洲国产精品成人久久久| 亚洲国产美女精品久久久久| 亚洲av无码久久忘忧草| 亚洲中文字幕无码久久2020| 亚洲av永久无码| 青青免费在线视频| 免费高清A级毛片在线播放| 一级毛片不卡免费看老司机|