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(co nst 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! 26 13026
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.programmin g?
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(co nst 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********@yah oo.com> wrote in message
news:db******** *************** **@posting.goog le.com... 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(co nst 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********@iah u.ca> wrote in message
news:RN******** ***********@new s04.bloor.is.ne t.cable.rogers. com... "pembed2003 " <pe********@yah oo.com> wrote in message news:db******** *************** **@posting.goog le.com... 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(co nst 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.
Nick Landsberg <hu*****@att.ne t> wrote in message news:<fA******* *************** @bgtnsc05-news.ops.worldn et.att.net>... The problem is probably not in strncmp(), but elsewhere.
No, I time the function and found out that the strcmp is the slowest
function of all. Without it (of course, I can't really remove it), the
whole function is much much faster. 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.
The function will eventually be moved to a server to do key/value pair
lookup so it will be called tens of thousands of times and that's why
I want to optimize it. Try a newsgroup dealing with algorithms. Maybe comp.programmin g?
If I can't get a good answer here, I will try there. Thanks!
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(co nst 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!
"Tom St Denis" <to********@iah u.ca> wrote in message news:<RN******* ************@ne ws04.bloor.is.n et.cable.rogers .com>... "pembed2003 " <pe********@yah oo.com> wrote in message news:db******** *************** **@posting.goog le.com... 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(co nst 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
Thanks but I can't use word-comparisons because I need to know if the
strings match exactly or not, regardless of case.
In article <db************ *************@p osting.google.c om>, pe********@yaho o.com (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(co nst char* s1,const char* s2){ while(*s1 && *s2 && *s1 == *s2){
This definitely does to many comparisons. If *s1 == *s2 then *s1 and *s2
are either both zero, or non of them is zero, so it is absolutely
pointless to check both of them.
s1++; s2++; } return *s1 == *s2; }
but I don't get much performance gain. Any suggestion?
Instead of worrying about the performance of strncmp, you should ask
yourself why you make so many calls to it that it matters at all.
What are you doing that requires millions of calls to strncmp?
By the way, why strncmp and not strcmp?
In article <rl************ *******@newssvr 27.news.prodigy .com>,
"Tim Prince" <tp*****@comput er.org> wrote: 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.
User code has to be portable. strncmp implementations don't.
In article <db************ **************@ posting.google. com>, pe********@yaho o.com (pembed2003) wrote: The function will eventually be moved to a server to do key/value pair lookup so it will be called tens of thousands of times and that's why I want to optimize it.
Oh shit.
If I do a key/value pair lookup, no comparison function will be called
tens of thousands of times. Less than two compares on the average. I
suggest you buy yourself a nice book about data structures.
"pembed2003 " <pe********@yah oo.com> wrote in message
news:db******** *************** **@posting.goog le.com... 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(co nst 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!
Have you tried forcing your compiler to use inline code rather than a
function call (either by implementing this as a Macro or by using the
'inline' keyword. You might find that setting up the stack frame (or
whatever) prior to making each call to your string_equal() function takes
just as long as the function itself.
Also it appears that you are implementing (a simplified version of) strcmp()
here NOT strncmp() and you are evaluating an extra condition in your loop
(drop the check on s2 for '\0'):.
int string_equal(co nst char * s1, const char * s2){
while(*s1 && * s1==*s2){
s1++; s2++;
}
return *s1==*s2;
}
Regarding memcmp() this is likely to be highly optimised and much faster
than strcmp(), but the problem is that you need to pass to it the length of
the strings that you are comparing. There might be a way you can make
use of memcmp() if you know that the strings are the same length. Do you
get this information somewhere earlier in your code (e.g when you first read
in the key value?). Otherwise try:
memcpy(s1,s2,st rlen(s1));
It might still be faster than doing strcmp() even with the extra strlen()
call...
Sean This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: jacob navia |
last post by:
What is the result of that?
I am reimplementing the C library, and I would like to know.
Now I have it to return zero. Is that correct?
The standard says:
>>
The strncmp function compares not more than n characters (characters
that follow a
|
by: Bert |
last post by:
I have a question about buffer overflows with strcmp or strncmp
(strnicmp). It's best described by providing an example
Example:
There is a string "sBuf" of length 5000. In a for loop from 0 to 5000,
several NUL terminated strings are compared, these strings vary in size
from 3 to 9.
sBuf is user input (a file), memory is dynamically allocated using
malloc (file length+1) and it is properly nul terminated. The other
|
by: Preets |
last post by:
Hi,
Can anybody please tell me the difference between memcmp() and
strncmp() functions ?
|
by: arunraj2002in |
last post by:
Hi All,
I have doubt in strncmp.
consider the following prog:
int main()
{
int i;
i = strncmp("Nstring","Astring",6);
|
by: Krumble Bunk |
last post by:
Hi all,
Having some trouble with a seemingly-simple task. Need to have a
basic CLI functionality as per:
prompt <- \n
prompt <- \n
promptquit <- should quit as per my snippet, but doesn't.
I've thrown a few random fflush()'s in, but no joy. I have never
| |
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
| |
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |