473,748 Members | 6,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strlen() speed test

I need to write a couple of my own string manipulation routines (e.g.
a strcpy() alternative that returns the number of chars copied). I've
started with one of the simpler functions, strlen(). I've written a
very simple version call StringLength(), but it performs significantly
slower than its CRT counterparts.

Here's my implementation:
inline unsigned int StringLength( const char *pszString )
{
unsigned int cch = 0;
while ( *pszString++ )
cch++;
return cch;
}

I've compared my version to strlen() and _mbslen() using my own timer
function, which is a wrapper around the Windows
QueryPerformanc eCounter() API.

My only guess is that the CRT versions use the processor registers for
its counters and my version uses RAM. (You can't make use of the
"register" keyword with the VC++ compiler.)

How can I make my string manipulation functions compete speedwise with
the CRT functions?

Jul 23 '05 #1
12 4424
Nollie wrote:
I need to write a couple of my own string manipulation routines (e.g.
a strcpy() alternative that returns the number of chars copied). I've
started with one of the simpler functions, strlen(). I've written a
very simple version call StringLength(), but it performs significantly
slower than its CRT counterparts.

Here's my implementation:
inline unsigned int StringLength( const char *pszString )
{
unsigned int cch = 0;
while ( *pszString++ )
cch++;
return cch;
}
How about:

inline unsigned int StringLength( const char *pszString )
{
const char* p = pszString;
while (*p) p++;
return p - pszString;
}
I've compared my version to strlen() and _mbslen() using my own timer
function, which is a wrapper around the Windows
QueryPerformanc eCounter() API.

My only guess is that the CRT versions use the processor registers for
its counters and my version uses RAM.
The compiler will decide what to use. Did you set maximum optimization
level?
(You can't make use of the "register" keyword with the VC++ compiler.)
Why?
How can I make my string manipulation functions compete speedwise with
the CRT functions?


There might be some optimizations that you can do (e.g. not go though each
single character in the loop but instead checking multiple characters at
once, handling the string end separately).
OTOH, the library routine possibly uses some special assembler instructions
to speed this up. Systems like x86 have special built-in string
instructions to search quickly for a specific value in an array.

Jul 23 '05 #2
>> (You can't make use of the "register" keyword with the VC++ compiler.)
Why? Dunno. Here's MS docs:
http://msdn.microsoft.com/library/de...er_keyword.asp
How about:
inline unsigned int StringLength( const char *pszString )
{
const char* p = pszString;
while (*p) p++;
return p - pszString;
} Thank you! Yes, this is a better algorithm. However, though it does
make StringLength() faster, it still does not rival the CRT functions.
The compiler will decide what to use. Did you set maximum optimization
level?

Yes, this is the answer. Sorry, I should have tried this before
posting. My timer only worked in unoptimized, DEBUG builds, but I've
rewritten it to work for all builds. StringLength() and strlen() now
use practically the exact same processing time.

However, now I have discovered another peculiarity...

Comparing 6 strlen() alternatives:
1. strlen() // CRT function
2. _tcslen() // redundant, maps to strlen
3. lstrlen() // Windows API
4. StringLength() // my function
5. _mbslen() // for multibyte strings
6. _mbstrlen() // for multibyte strings

Every one uses almost the exact same processing time, except _mbslen()
and _mbstrlen(), which are almost twice as fast as the others. I've
arranged the test so that the _mbs* functions are tested first, last,
and in between, so caching is not an issue.

It seems to me the _mbs* should be slower because they have to check
for double-byte character, but they are indeed quite faster. Why might
this be?

Jul 23 '05 #3

Nollie wrote:
(You can't make use of the
"register" keyword with the VC++ compiler.)

Says who?

Brian

Jul 23 '05 #4
>Says who?

Topic = strlen() speed test.

http://msdn.microsoft.com/library/de...er_keyword.asp
Jul 23 '05 #5
>I need to write a couple of my own string manipulation routines (e.g.
a strcpy() alternative that returns the number of chars copied). I've
started with one of the simpler functions, strlen(). I've written a
very simple version call StringLength(), but it performs significantly
slower than its CRT counterparts.


Which it may well do.
Vendor implementations may well be written in assembler.
In particular for 32-bit Intel, there is a trick where it is possible to
process 4 bytes at once doing, bit manpulation to see if the '\0' is in any
of the 4 bytes. And if MMX is available, 8 bytes can be processed at once.
And for either, both are likely to faster than your code.

But in any case, why bother.

Stephen Howe
Jul 23 '05 #6
Nollie wrote:
I need to write a couple of my own string manipulation routines (e.g.
a strcpy() alternative that returns the number of chars copied). I've
started with one of the simpler functions, strlen(). I've written a
very simple version call StringLength(), but it performs significantly
slower than its CRT counterparts.

Here's my implementation:
inline unsigned int StringLength( const char *pszString )
{
unsigned int cch = 0;
while ( *pszString++ )
cch++;
return cch;
}

I've compared my version to strlen() and _mbslen() using my own timer
function, which is a wrapper around the Windows
QueryPerformanc eCounter() API.

My only guess is that the CRT versions use the processor registers for
its counters and my version uses RAM. (You can't make use of the
"register" keyword with the VC++ compiler.)

How can I make my string manipulation functions compete speedwise with
the CRT functions?


Here are some {processor specific} ideas:
1. Rewrite in assembly using processor string instructions.
2. Instead of fetching one char at a time, fetch as many
as will fit into the processor's native register.
For example, if the processor register is 32-bits and
a char is 8-bits, then fetch 4 chars at a time.

The reasoning is the time for a "fetch" from memory
costs the same whether it is one char or one word.
So maximize this time.

3. If the processor has the ability, then fill as many
registers as possible during one fetch instruction
cycle. Note that there is a penalty for short strings,
because there is the wasted extra data.

So, why are you wasting your time optimizing library
routines?
Does your program work correctly?
Are the user's complaining about the speed?
Is the program missing events due to slowness?
Is your program causing the computer to miss events
because it is too slow, or locking up resources?

Don't optimize until the program works correctly.

I had a program that programmed a Flash memory device,
which took 5 minutes (but it worked correctly). I
reread the data sheet and optimized it, then the program
took 30 seconds. And yes, developers were complaining
about the time.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.l earn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
Jul 23 '05 #7

Nollie wrote:

[context restored]
(You can't make use of the
"register" keyword with the VC++ compiler.)
Says who?


[MSDN link]
So by "make use of" you mean "can't get a register". Well, I'll break
the news that it's the same for almost every modern implementation.
Microsoft is just upfront about it.

Brian

Jul 23 '05 #8
>So, why are you wasting your time optimizing library
routines?


It all started with a need for a strcpy() alternative that returns the
number of characters copied. But I actually have an OCD complex where
I have to know exactly how everything works. ;-) The blackbox concept
of programming with libraries and modules irks me to no end.

I have, however, decided that the you guys are right, and that the CRT
functions will serve my purpose just fine. Thanks for all the input.
This newsgroup really is a great example of how helpful Usenet can be.
Jul 23 '05 #9
Nollie wrote:
So, why are you wasting your time optimizing library
routines?


It all started with a need for a strcpy() alternative that returns the
number of characters copied. But I actually have an OCD complex where
I have to know exactly how everything works. ;-) The blackbox concept
of programming with libraries and modules irks me to no end.


Then you should use more opensource/free software that comes with complete
source code. At least you don't need to write things yourself then, but
just look into the sources to "know exactly how everything is working". ;-)

Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
7439
by: Jordanakins | last post by:
Usenet, I am currently working on my website and am needing to detect the connection speed of the client. I would like to do this in PHP and not use any other languages. This makes it a bit more complicated. I know it is possiable to do this in PHP but I can't think of how. All I need to figure out is if they are on dial up or not. It doesn't have to be 100% accurate but at least 50% accurate. Any help is greatly appreciated.
45
11719
by: Matt Parkins | last post by:
Hi, (I realise this probably isn't precisely the right group for this - could someone direct me to the appropriate group to post this question? - thanks !) I'm using Visual C++ 2005 Express Edition Beta (free download from MS - hooray!), and everything works fine, except I get warnings back on the use of some functions, strlen() for example, saying that the function has been deprecated - although they do still work (which is I guess...
81
7327
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there be any advantage in having strcat and strcpy return a pointer to the "end" of the destination string rather than returning a
33
2966
by: apropo | last post by:
what is wrong with this code? someone told me there is a BAD practice with that strlen in the for loop, but i don't get it exactly. Could anyone explain me in plain english,please? char *reverse(char *s) { int i; char *r; if(!s) return NULL;//ERROR r=calloc(strlen(s)+1,sizeof(char));
66
7776
by: roy | last post by:
Hi, I was wondering how strlen is implemented. What if the input string doesn't have a null terminator, namely the '\0'? Thanks a lot Roy
9
3558
by: No Such Luck | last post by:
I have a function which requires me to loop from the end of a string to the beginning on a char by char basis: int foo (char string) { unsigned int i; for(i = strlen(string); i >= 0; i--) { /* do something */
11
9215
by: Sezai YILMAZ | last post by:
Hello I need high throughput while inserting into PostgreSQL. Because of that I did some PostgreSQL insert performance tests. ------------------------------------------------------------ -- Test schema create table logs ( logid serial primary key, ctime integer not null,
44
25081
by: sam_cit | last post by:
Hi Everyone, I tried the following program unit in Microsoft Visual c++ 6.0 and the program caused unexpected behavior, #include <stdio.h> #include <string.h> int main() {
31
470
by: Jean-Marc Bourguet | last post by:
jacob navia <jacob@nospam.comwrites: I'd first align and then use that. You may get a trap with unaligned access even on machine where unaligned access doesn't normally trap if you stump on a page boundary with the next page not mapped in memory. And some machine allows unaligned access but at a performance penality, which would be against the goal of using that trick.
0
8991
marktang
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...
0
8831
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,...
0
9548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9249
tracyyun
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...
1
6796
isladogs
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...
0
6076
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();...
0
4876
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.