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

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

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

    莊周夢蝶

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

    今天長見識(shí)了

    Posted on 2007-11-29 18:17 dennis 閱讀(1185) 評(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è)方法簡單,但是相當(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无码少妇电影☆| 亚洲黄色片免费看| 免费A级毛片无码视频| 久久久久免费看成人影片| 免费被黄网站在观看| 九九九精品视频免费| 亚洲成aⅴ人在线观看| 久久免费视频精品| 亚洲国产精品无码中文lv| 成人免费在线看片| 免费无码AV一区二区| 亚洲视频免费在线看| 免费久久精品国产片香蕉| 人妻无码久久一区二区三区免费 | 免费一级毛片在线播放不收费| 国产好大好硬好爽免费不卡| 亚洲av日韩综合一区二区三区| 亚洲av永久无码精品古装片| 日韩在线视频免费看| 亚洲av永久无码精品秋霞电影秋| 亚洲人成中文字幕在线观看| 免费无码黄网站在线观看| 久久久久久久久久国产精品免费| 国产AV无码专区亚洲AV麻豆丫| 亚洲精品免费观看| 亚洲乱亚洲乱少妇无码| 中文字幕影片免费在线观看| 亚洲欧美综合精品成人导航| 国产免费看插插插视频| 特级毛片A级毛片免费播放| 亚洲天堂中文资源| 亚洲真人无码永久在线| 国产在线一区二区综合免费视频| 亚洲色中文字幕在线播放| 午夜亚洲国产成人不卡在线| 黄色视频在线免费观看| 亚洲AV成人精品日韩一区| 亚洲人成电影在线观看青青| 亚洲av无码精品网站| 久久亚洲国产精品五月天婷| 免费永久国产在线视频|