Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Optimized code for finding string length

Reply
Thread Tools

Optimized code for finding string length

 
 
SSG
Guest
Posts: n/a
 
      07-07-2005
Hai All!

I need the optimized code for finding the string length. i dont want to
use strlen function.........

can anyone know reply........

By
S.S.G

 
Reply With Quote
 
 
 
 
Erik de Castro Lopo
Guest
Posts: n/a
 
      07-07-2005
SSG wrote:
>
> Hai All!
>
> I need the optimized code for finding the string length. i dont want to
> use strlen function.........


Why not? Think you can do better?

Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo (Yes it's valid)
+-----------------------------------------------------------+
Moore's Law: hardware speed doubles every 18 months
Gates' Law: software speed halves every 18 months
 
Reply With Quote
 
 
 
 
Mark
Guest
Posts: n/a
 
      07-07-2005
"SSG" <> wrote in message
news: oups.com...
> Hai All!
>
> I need the optimized code for finding the string length. i dont want to
> use strlen function.........

Why not? Homework problem?
Do your own homework Eshita.

> can anyone know reply........
> By
> S.S.G


I'm sure the people at know the answers to your
questions... ask them.


 
Reply With Quote
 
chellappa
Guest
Posts: n/a
 
      07-07-2005
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <limits.h>

int main()
{
long value;
char src[]="71";
char *err;
errno = 0;
value = strtol(src, &err, ;
perror("strtol");
printf("%ld %ld\n", value, LONG_MAX);
printf("%d\n", *err);
return 0;
}

 
Reply With Quote
 
akarl
Guest
Posts: n/a
 
      07-07-2005
SSG wrote:
> Hai All!
>
> I need the optimized code for finding the string length. i dont want to
> use strlen function.........
>
> can anyone know reply........
>
> By
> S.S.G


I doubt the strlen function can be optimized. After all we have to go
through the character array until we find 0. Try to optimize the
function that needs the string length. Are you sure you really need it
in advance?

 
Reply With Quote
 
websnarf@gmail.com
Guest
Posts: n/a
 
      07-07-2005
SSG wrote:
> I need the optimized code for finding the string length. i dont want to
> use strlen function.........


The easiest way to optimize knowing the length of strings, is to simply
have them procomputed and lying around along with the string. See:
http://bstring.sf.net/ for an example of this. The Better String
Library generally beats the C-library in terms of performance precisely
because it never performs strlen()'s or implicitely equivalent
computations (except when converting from legacy char * strings.)

If you must stick with char * strings, then see Example #5 on my
assembly examples page:
http://www.azillionmonkeys.com/qed/asmexample.html and look for the
last code snippet in that example. Its C code that will work on most
systems, and beats most compilers (though I am told the Sun compiler
implements essentially this trick already, thus having the same
performance).

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      07-07-2005
"chellappa" <> writes:
> #include <stdio.h>
> #include <errno.h>
> #include <stdlib.h>
> #include <limits.h>
>
> int main()
> {
> long value;
> char src[]="71";
> char *err;
> errno = 0;
> value = strtol(src, &err, ;
> perror("strtol");
> printf("%ld %ld\n", value, LONG_MAX);
> printf("%d\n", *err);
> return 0;
> }


"err" is a poor name for something to be used for the second argument
in a call to strtol().

You call perror() without checking whether there was actually an
error.

The program interprets the string "71" as an octal number. I suppose
that might be a useful thing to do in some circumstances, but I'm at a
loss to understand the point.

The article to which you're responding asked about finding the length
of a string. Your program doesn't do that.

As usual, you've posted without providing any context from the
previous article.

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
performance measure for optimized c++ code gautamcool88@gmail.com C++ 3 07-10-2008 09:27 AM
String exceeding length - Getting absolute string length james.w.appleby@gmail.com Java 5 01-11-2007 12:07 AM
How can I write highly optimized C code for reading and writing? slahiri@tampabay.rr.com C Programming 4 02-11-2006 02:10 PM
left(string, length) or right(string, length)? Sam ASP .Net 3 02-17-2005 12:01 PM
How to get length of string? length() problems Mitchua Perl 5 07-17-2003 12:08 AM



Advertisments