• <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无码专区喷水| 久久综合狠狠综合久久| 久久精品国产亚洲AV高清热| 久久受www免费人成_看片中文| 久久99国产一区二区三区| 久久青青草原精品影院| 久久久久综合中文字幕| 久久午夜福利无码1000合集| 久久99国产亚洲高清观看首页| 久久精品国产免费观看| 亚洲狠狠久久综合一区77777| 伊人色综合久久天天人守人婷| 久久久久综合网久久| 欧美国产成人久久精品| 久久w5ww成w人免费| 亚洲欧美日韩久久精品| 久久99精品国产| 人妻无码精品久久亚瑟影视| 99热都是精品久久久久久| 久久国产色AV免费观看| 九九久久精品无码专区| 久久91精品国产91久久小草| 国产69精品久久久久观看软件| 99久久国产综合精品网成人影院| 99久久国产精品免费一区二区| 中文字幕精品无码久久久久久3D日动漫| 亚洲午夜久久久久久久久电影网 | 69久久夜色精品国产69| 波多野结衣AV无码久久一区| 久久AAAA片一区二区| 91精品国产91久久久久久蜜臀| 久久精品九九亚洲精品| 国内精品久久久久影院薰衣草| 久久九色综合九色99伊人| 久久精品亚洲乱码伦伦中文| 久久国产欧美日韩精品| 久久婷婷激情综合色综合俺也去| 中文无码久久精品| 亚洲AV无码1区2区久久| 久久精品国产只有精品2020| 久久久噜噜噜www成人网|