Optimized code for finding string length

Discussion in 'C Programming' started by SSG, Jul 7, 2005.

  1. SSG

    SSG Guest

    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
    SSG, Jul 7, 2005
    #1
    1. Advertising

  2. 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
    Erik de Castro Lopo, Jul 7, 2005
    #2
    1. Advertising

  3. SSG

    Mark Guest

    "SSG" <> wrote in message
    news:...
    > 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.
    Mark, Jul 7, 2005
    #3
  4. SSG

    chellappa Guest

    #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, 8);
    perror("strtol");
    printf("%ld %ld\n", value, LONG_MAX);
    printf("%d\n", *err);
    return 0;
    }
    chellappa, Jul 7, 2005
    #4
  5. SSG

    akarl Guest

    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?
    akarl, Jul 7, 2005
    #5
  6. SSG

    Guest

    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/
    , Jul 7, 2005
    #6
  7. "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, 8);
    > 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) <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.
    Keith Thompson, Jul 7, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mitchua
    Replies:
    5
    Views:
    2,715
    Eric J. Roode
    Jul 17, 2003
  2. Sam
    Replies:
    3
    Views:
    14,081
    Karl Seguin
    Feb 17, 2005
  3. Replies:
    5
    Views:
    655
    John W. Kennedy
    Jan 11, 2007
  4. Replies:
    4
    Views:
    360
    Christian Bau
    Feb 11, 2006
  5. Replies:
    3
    Views:
    467
    James Kanze
    Jul 10, 2008
Loading...

Share This Page