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 放屁阿狗 阅读(4007) 评论(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
这位老兄说的非常有道理  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理