strncmp表现 [英] strncmp performance

查看:222
本文介绍了strncmp表现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个应用程序,我广泛使用strncmp函数

比较2个字符串,看它们是否相同。有谁知道

更快替代strncmp?我注意到有一个memncmp函数

这是非常快但它不像strncmp一样工作所以我

我认为我不能使用它。我也尝试自己编写string_equal

函数:


int string_equal(const char * s1,const char * s2){

while(* s1&& * s2&& * s1 == * s2){

s1 ++; s2 ++;

}

返回* s1 == * s2;

}


但是我没有太多的性能提升。有什么建议吗?


谢谢!

解决方案

问题可能不在strncmp()中,而是在其他地方。


当有许多字符串需要比较时,线性比较字符串是一个昂贵的命题

。如果只有少数(10个左右)只有少数(10个左右),那就不错了,如果有
,那么strncmp()没有智能的它背后的
算法(例如二进制搜索)可能是

不是要走的路。


尝试一个处理算法的新闻组。也许

comp.programming?


pembed2003写道:


我有一个应用程序我广泛使用strncmp函数来比较2个字符串,看它们是否相同。有谁知道更快地替换strncmp?我注意到有一个memncmp函数
这是非常快的,但它不像strncmp那样工作,所以我不认为我可以使用它。我也尝试自己编写string_equal
函数:

int string_equal(const char * s1,const char * s2){
while(* s1&& * s2&& * s1 == * s2){
s1 ++; s2 ++;
}
返回* s1 == * s2;
}

但我没有太多的性能提升。有什么建议吗?

谢谢!




-

不可能做任何万无一失的事,因为傻瓜是这样的b $ b巧妙 - A. Bloch




" pembed2003" < PE ******** @ yahoo.com>在消息中写道

新闻:db ************************* @ posting.google.co m ... < blockquote class =post_quotes>
我有一个应用程序,我广泛使用strncmp函数来比较2个字符串,看它们是否相同。有谁知道更快地替换strncmp?我注意到有一个memncmp函数
这是非常快的,但它不像strncmp那样工作,所以我不认为我可以使用它。我也尝试自己编写string_equal
函数:

int string_equal(const char * s1,const char * s2){
while(* s1&& * s2&& * s1 == * s2){
s1 ++; s2 ++;
}
返回* s1 == * s2;
}

但我没有太多的性能提升。有什么建议?




可能是因为本机strncmp是高度优化的代码?例如。做

字比较而不是字符比较可以很容易地给你最多

2x / 4x / 8x / etc加速[基于你的机器寄存器大小]。它还使代码更复杂,但是对于超过十几个字符串的字符串

更快。


Tom




Tom St Denis <到******** @ iahu.ca>在消息中写道

新闻:RN ******************* @ news04.bloor.is.net.cab le.rogers.com .. 。


" pembed2003" < PE ******** @ yahoo.com>在消息中写道
新闻:db ************************* @ posting.google.co m ...


我有一个应用程序,我广泛使用strncmp函数来比较2个字符串,看它们是否相同。有谁知道更快地替换strncmp?我注意到有一个memncmp函数
这是非常快的,但它不像strncmp那样工作,所以我不认为我可以使用它。我也尝试自己编写string_equal
函数:

int string_equal(const char * s1,const char * s2){
while(* s1&& * s2&& * s1 == * s2){
s1 ++; s2 ++;
}
返回* s1 == * s2;
}

但我没有太多的性能提升。有什么建议吗?
可能是因为本机strncmp是高度优化的代码?例如。进行单词比较而不是字符比较可以轻松地为您提供2x / 4x / 8x / etc加速[基于您的机器寄存器大小]。它还使代码更复杂,但是对于超过十几个字符串的字符串



字符更快。

Tom



将Tom的建议与适合目标的展开相结合

架构应该可以在更长的字符串上提供额外的性能。注意

即使在具有对齐修正的架构上,多字节比较也不会在没有调整

对齐的情况下获得速度。没有

便携式C方式来优化给定体系结构的这种方式,然后这个

将使您在最流行的体系结构上超越标准C.


Hi,
I have an application where I use the strncmp function extensively to
compare 2 string to see if they are the same or not. Does anyone know
a faster replacement for strncmp? I notice there is a memncmp function
which is very fast but it doesn''t work the same like strncmp so I
don''t think I can use it. I also tried to write the string_equal
function myself like:

int string_equal(const char* s1,const char* s2){
while(*s1 && *s2 && *s1 == *s2){
s1++; s2++;
}
return *s1 == *s2;
}

but I don''t get much performance gain. Any suggestion?

Thanks!

解决方案

The problem is probably not in strncmp(), but elsewhere.

Comparing strings linearly is an expensive proposition
when there are many strings to be compared. If there''s
only a handful (10 or so) it''s not bad, if there are
thousands, then strncmp() without an intelligent
algorithm (e.g. binary search) behind it is probably
not the way to go.

Try a newsgroup dealing with algorithms. Maybe
comp.programming?

pembed2003 wrote:

Hi,
I have an application where I use the strncmp function extensively to
compare 2 string to see if they are the same or not. Does anyone know
a faster replacement for strncmp? I notice there is a memncmp function
which is very fast but it doesn''t work the same like strncmp so I
don''t think I can use it. I also tried to write the string_equal
function myself like:

int string_equal(const char* s1,const char* s2){
while(*s1 && *s2 && *s1 == *s2){
s1++; s2++;
}
return *s1 == *s2;
}

but I don''t get much performance gain. Any suggestion?

Thanks!



--
"It is impossible to make anything foolproof because fools are so
ingenious" - A. Bloch



"pembed2003" <pe********@yahoo.com> wrote in message
news:db*************************@posting.google.co m...

Hi,
I have an application where I use the strncmp function extensively to
compare 2 string to see if they are the same or not. Does anyone know
a faster replacement for strncmp? I notice there is a memncmp function
which is very fast but it doesn''t work the same like strncmp so I
don''t think I can use it. I also tried to write the string_equal
function myself like:

int string_equal(const char* s1,const char* s2){
while(*s1 && *s2 && *s1 == *s2){
s1++; s2++;
}
return *s1 == *s2;
}

but I don''t get much performance gain. Any suggestion?



Probably because the native strncmp is highly optimized code? E.g. doing
word-comparisons instead of char comparisons can easily give you upto
2x/4x/8x/etc speedup [based on your machine register size]. It also makes
the code a bit more complex but for strings longer than a dozen or so chars
is faster.

Tom



"Tom St Denis" <to********@iahu.ca> wrote in message
news:RN*******************@news04.bloor.is.net.cab le.rogers.com...


"pembed2003" <pe********@yahoo.com> wrote in message
news:db*************************@posting.google.co m...

Hi,
I have an application where I use the strncmp function extensively to
compare 2 string to see if they are the same or not. Does anyone know
a faster replacement for strncmp? I notice there is a memncmp function
which is very fast but it doesn''t work the same like strncmp so I
don''t think I can use it. I also tried to write the string_equal
function myself like:

int string_equal(const char* s1,const char* s2){
while(*s1 && *s2 && *s1 == *s2){
s1++; s2++;
}
return *s1 == *s2;
}

but I don''t get much performance gain. Any suggestion?
Probably because the native strncmp is highly optimized code? E.g. doing
word-comparisons instead of char comparisons can easily give you upto
2x/4x/8x/etc speedup [based on your machine register size]. It also makes
the code a bit more complex but for strings longer than a dozen or so


chars is faster.

Tom


Combining Tom''s suggestion with unrolling appropriate to the target
architecture should give additional performance on longer strings. Note
that the multi-byte comparisons won''t gain speed without adjustments for
alignment, even on architectures which have alignment fixup. There''s no
portable C way to optimize this for a given architecture, and then this
takes you beyond standard C on the most popular architectures.


这篇关于strncmp表现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆