• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            socketref,再見!高德

            https://github.com/adoggie

              C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
              246 Posts :: 4 Stories :: 312 Comments :: 0 Trackbacks

            常用鏈接

            留言簿(54)

            我參與的團隊

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜


            return true if equal

             1 bool strcmp( char* d,char * s){
             2     if( d==s) return true;
             3     while*d==*&& *&& *s){
             4         d++;s++;
             5     }
             6     if*d==*&& *d==0){
             7         return true;
             8     }
             9     return false;
            10 }


            posted on 2008-05-05 02:57 放屁阿狗 閱讀(4043) 評論(15)  編輯 收藏 引用 所屬分類: unix/linux/solaris/sco-unix/novell

            Feedback

            # re: 隨便寫個strcmp()函數,看看大家能否有更簡潔的實現 2008-05-05 07:28 herculesinchina
            char tmp;
            char not_equal = 1;
            if((d == s) return true;
            if((d == NULL) || (s == NULL)) return false;

            do{
            tmp = !(*s++);
            not_equal = !(tmp == *d++);
            }while(!(tmp - not_equal));

            if(not_equal)
            return false
            return true

            不過 最高效的實現還是嵌入匯編  回復  更多評論
              

            # re: 隨便寫個strcmp()函數,看看大家能否有更簡潔的實現 2008-05-05 07:30 herculesinchina
            正確的程序
            char tmp;
            char not_equal = 1;
            if((d == s) return true;
            if((d == NULL) || (s == NULL)) return false;

            do{
            tmp = (*s++);
            not_equal = !(tmp == *d++);
            }while(!((!tmp) - not_equal));

            if(not_equal)
            return false
            return true  回復  更多評論
              

            # re: 隨便寫個strcmp()函數,看看大家能否有更簡潔的實現 2008-05-05 08:32 不懂
            樓主要求的是簡潔,呵呵,一般來說都是要求快速,所以樓主這個命題就算是實現了也沒什么用  回復  更多評論
              

            # re: 隨便寫個strcmp()函數,看看大家能否有更簡潔的實現[未登錄] 2008-05-05 08:46 len
            crt中strcmp()返回的是整數值,這樣就可以表示小于,等于,大于.
            貼段vc中crt實現吧

            int __cdecl strcmp (
            const char * src,
            const char * dst
            )
            {
            int ret = 0 ;

            while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
            ++src, ++dst;

            if ( ret < 0 )
            ret = -1 ;
            else if ( ret > 0 )
            ret = 1 ;

            return( ret );
            }
              回復  更多評論
              

            # re: 隨便寫個strcmp()函數,看看大家能否有更簡潔的實現 2008-05-05 09:51 孤帆1
            建議看看ms的源碼.  回復  更多評論
              

            # re: 隨便寫個strcmp()函數,看看大家能否有更簡潔的實現 2008-05-05 12:00 螞蟻終結者
            作為庫函數效率還是比簡潔重要,建議看下VC CRT的匯編代碼。  回復  更多評論
              

            # re: 隨便寫個strcmp()函數,看看大家能否有更簡潔的實現 2008-05-05 12:20 zhp
            我也寫個吧:
            bool strcmp( const char* d,const char * s)
            {
            if( d==s) return true;
            while( *d++==*s++ );
            return !(*d||*s);
            }
              回復  更多評論
              

            # re: 隨便寫個strcmp()函數,看看大家能否有更簡潔的實現 2008-05-05 12:24 zhp
            不追求效率還可以再去一行:
            bool strcmp( const char* d,const char * s)
            {
            while( *d++==*s++ );
            return !(*d||*s);
            }
              回復  更多評論
              

            # re: 隨便寫個strcmp()函數,看看大家能否有更簡潔的實現 2008-05-05 16:19 肥仔
            要有大于0,小于0,等于0的返回值啊,怎么才true false呢?

            Return Value
            The return value for each of these functions indicates the lexicographic relation of string1 to string2.

            Value Relationship of string1 to string2
            < 0 string1 less than string2
            0 string1 identical to string2
            > 0 string1 greater than string2
              回復  更多評論
              

            # re: 隨便寫個strcmp()函數,看看大家能否有更簡潔的實現 2008-05-05 19:21 vitacy
            assert(d && s);
            if(d==s) return true;
            if(len(d) != len(s) ) return false;
            while(*d && *d++ == *s++ )

            return ! *d;  回復  更多評論
              

            # re: 隨便寫個strcmp()函數,看看大家能否有更簡潔的實現 2008-05-05 21:21 Wang Feng
            int
            strcmp(const char *s1, const char *s2)
            {
            while (*s1 == *s2)
            {
            if (*s1 == 0)
            return 0;
            ++s1;
            ++s2;
            }
            return *(unsigned const char *)s1 - *(unsigned const char *)(s2);
            }
              回復  更多評論
              

            # re: 隨便寫個strcmp()函數,看看大家能否有更簡潔的實現 2008-05-05 22:00 herculesinchina
            個人感覺這段VC CRT的代碼效率不如我的高。
            下面是對while循環體的編譯反匯編結果。編譯選項為:
            gcc -O3
            ============================================
            VC CRT代碼循環體反匯編結果
            ----------------------------------------------------------
            80483b4: 0f b6 13 movzbl (%ebx),%edx
            80483b7: 0f b6 01 movzbl (%ecx),%eax
            80483ba: 29 c2 sub %eax,%edx
            80483bc: 75 21 jne 80483df <strcmp+0x3f>
            80483be: 80 39 00 cmpb $0x0,(%ecx)
            80483c1: 75 10 jne 80483d3 <strcmp+0x33>
            80483c3: eb 1a jmp 80483df <strcmp+0x3f>
            80483c5: 0f b6 41 01 movzbl 0x1(%ecx),%eax
            80483c9: 83 c3 01 add $0x1,%ebx
            80483cc: 83 c1 01 add $0x1,%ecx
            80483cf: 84 c0 test %al,%al
            80483d1: 74 0c je 80483df <strcmp+0x3f>
            80483d3: 0f b6 53 01 movzbl 0x1(%ebx),%edx
            80483d7: 0f b6 41 01 movzbl 0x1(%ecx),%eax
            80483db: 29 c2 sub %eax,%edx
            80483dd: 74 e6 je 80483c5 <strcmp+0x25>
            共16條語句
            =================================================
            我的代碼反匯編結果
            --------------------------------------------------------------
            80483c0: 0f b6 01 movzbl (%ecx),%eax
            80483c3: 83 c1 01 add $0x1,%ecx
            80483c6: 3a 03 cmp (%ebx),%al
            80483c8: 0f 95 c2 setne %dl
            80483cb: 83 c3 01 add $0x1,%ebx
            80483ce: 84 c0 test %al,%al
            80483d0: 89 d6 mov %edx,%esi
            80483d2: 0f 94 c0 sete %al
            80483d5: 0f be d2 movsbl %dl,%edx
            80483d8: 0f b6 f8 movzbl %al,%edi
            80483db: 39 d7 cmp %edx,%edi
            80483dd: 74 e1 je 80483c0 <strcmp+0x20>
            ==================================================
            從指令函數上看,循環體少了4條指令,且中間沒有任何跳轉指令,不會影響指令流水線執行。運行效率應該比VC CRT版本高得多。
              回復  更多評論
              

            # re: 隨便寫個strcmp()函數,看看大家能否有更簡潔的實現 2008-05-05 22:32 herculesinchina
            我的代碼好體現在三點:
            1、訪存次數少(一次循環僅訪存兩次)
            對于VC代碼,雖然cache一般都會命中,訪問速度肯定比訪問寄存器慢些
            2、循環體內無跳轉指令,利于流水線操作
            3、指令少。
              回復  更多評論
              

            # re: 隨便寫個strcmp()函數,看看大家能否有更簡潔的實現 2008-05-07 10:27 Louix
            strcmp表面上看是比較字符串,說白了就是比較兩段內存,為什么只用char *呢?用int *或者__int64 *才會帶來質的飛躍,對于字符串結尾0的處理參考strlen。  回復  更多評論
              

            # re: 隨便寫個strcmp()函數,看看大家能否有更簡潔的實現 2008-05-08 00:27 放屁阿狗
            @Louix
            這位老兄說的非常有道理  回復  更多評論
              

            久久人人爽人人爽人人片AV高清| 欧美精品国产综合久久| a高清免费毛片久久| 欧美777精品久久久久网| 久久久中文字幕日本| 久久精品青青草原伊人| 久久国产乱子伦免费精品| 久久99精品免费一区二区| 精品综合久久久久久97| 久久91精品久久91综合| 中文字幕无码av激情不卡久久| 久久久一本精品99久久精品88 | 欧美日韩久久中文字幕| 久久99亚洲网美利坚合众国| 无码任你躁久久久久久老妇| 久久国产精品成人影院| 久久久久久国产a免费观看黄色大片| 久久精品蜜芽亚洲国产AV| 亚洲国产精品无码久久青草 | 久久精品无码专区免费| 婷婷五月深深久久精品| 91亚洲国产成人久久精品网址 | 伊人色综合九久久天天蜜桃 | 久久国产欧美日韩精品| 国产日韩欧美久久| 久久99精品国产99久久| 久久精品国产色蜜蜜麻豆| 欧美性大战久久久久久| 欧美一区二区精品久久| 久久久精品免费国产四虎| 久久Av无码精品人妻系列| 欧美牲交A欧牲交aⅴ久久| 亚洲精品tv久久久久久久久 | 久久久久久极精品久久久| 99久久99久久| 久久精品男人影院| 精品久久人妻av中文字幕| 久久一日本道色综合久久| 青草国产精品久久久久久| 久久综合给久久狠狠97色| 久久久久高潮毛片免费全部播放|