String consisting of spaces

Discussion in 'Java' started by Andrew Smith, Oct 17, 2003.

  1. Andrew Smith

    Andrew Smith Guest

    I have a string, s, and I want another string, t, which has s.length() ==
    t.length() but t only consists of white space ?

    Is there a faster way than this.

    String t = ""; // (or stringbuffer if t is big enough to make it worthwhile)
    for (int n=0;n < s.length(); n++) {
    t += " ";
    }
     
    Andrew Smith, Oct 17, 2003
    #1
    1. Advertising

  2. Andrew Smith

    VisionSet Guest

    "Andrew Smith" <> wrote in message
    news:...
    > I have a string, s, and I want another string, t, which has s.length() ==
    > t.length() but t only consists of white space ?
    >
    > Is there a faster way than this.
    >
    > String t = ""; // (or stringbuffer if t is big enough to make it

    worthwhile)
    > for (int n=0;n < s.length(); n++) {
    > t += " ";
    > }
    >


    In a word... YES. I'm afraid your method is very bad.
    It will internally create s.length StringBuffers, when one would have done.

    > or stringbuffer if t is big enough to make it worthwhile


    Yes t is always big enough to make it worthwhile.

    But this would be even better. Just primative char[] and your final String:

    String s = "myString";

    char[] spaces = new char[s.length()];

    for(int i = 0 ; i<spaces.length; i++)
    spaces = ' ';

    String t = new String(spaces);

    System.out.println(">"+t+"<");
    System.out.println(">"+s+"<");


    --
    Mike W
     
    VisionSet, Oct 17, 2003
    #2
    1. Advertising

  3. Andrew Smith

    Guest Guest

    Andrew Smith <> wrote:
    > I have a string, s, and I want another string, t, which has s.length() ==
    > t.length() but t only consists of white space ?


    > Is there a faster way than this.


    If there is a maximum length for s you could preconstruct a String
    of that length that contains only spaces and then just extract
    a substring of the appropriate length when you need it. May or
    may not be faster depending on the usage.

    --arne

    DISCLAIMER: These opinions and statements are those of the author and
    do not represent any views or positions of the Hewlett-Packard Co.
     
    Guest, Oct 17, 2003
    #3
  4. Andrew Smith

    Paul Lutus Guest

    Andrew Smith wrote:

    > I have a string, s, and I want another string, t, which has s.length() ==
    > t.length() but t only consists of white space ?
    >
    > Is there a faster way than this.
    >
    > String t = ""; // (or stringbuffer if t is big enough to make it
    > worthwhile) for (int n=0;n < s.length(); n++) {
    > t += " ";
    > }


    Yes, in fact that is extremely inefficient for some technical reasons. This
    is much faster:

    String original = "This is my test string.";
    String replacement = original.replaceAll("."," ");

    System.out.println("[" + original + "]");
    System.out.println("[" + replacement + "]");
    System.out.println(original.length() - replecement.length());

    Result:

    [This is my test string.]
    [ ]
    0

    Before the purists reply, remember -- the replaceAll() method is written in
    native code, and "faster" was the criterion.

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Oct 17, 2003
    #4
  5. "Andrew Smith" <> wrote in message
    news:...
    > I have a string, s, and I want another string, t, which has s.length() ==
    > t.length() but t only consists of white space ?
    >
    > Is there a faster way than this.
    >
    > String t = ""; // (or stringbuffer if t is big enough to make it

    worthwhile)
    > for (int n=0;n < s.length(); n++) {
    > t += " ";
    > }


    If you know the maximum length of s beforehand, this would work:
    String spaces = " "; // MAX_LENGTH spaces
    t = spaces.substring(0, s.length()); // this may be off by one, I'm never
    sure

    HTH,
    Michiel
     
    Michiel Konstapel, Oct 17, 2003
    #5
  6. Andrew Smith

    VisionSet Guest

    "Paul Lutus" <> wrote in message
    news:...
    > This
    > is much faster:
    >
    > String original = "This is my test string.";
    > String replacement = original.replaceAll("."," ");
    >
    > System.out.println("[" + original + "]");
    > System.out.println("[" + replacement + "]");
    > System.out.println(original.length() - replecement.length());
    >
    > Result:
    >
    > [This is my test string.]
    > [ ]
    > 0
    >
    > Before the purists reply, remember -- the replaceAll() method is written

    in
    > native code, and "faster" was the criterion.
    >


    That's funny, I make your method 25x slower than my char[] one :)

    --
    Mike W
     
    VisionSet, Oct 17, 2003
    #6
  7. Andrew Smith

    Paul Lutus Guest

    VisionSet wrote:

    >
    > "Paul Lutus" <> wrote in message
    > news:...
    >> This
    >> is much faster:
    >>
    >> String original = "This is my test string.";
    >> String replacement = original.replaceAll("."," ");
    >>
    >> System.out.println("[" + original + "]");
    >> System.out.println("[" + replacement + "]");
    >> System.out.println(original.length() - replecement.length());
    >>
    >> Result:
    >>
    >> [This is my test string.]
    >> [ ]
    >> 0
    >>
    >> Before the purists reply, remember -- the replaceAll() method is written

    > in
    >> native code, and "faster" was the criterion.
    >>

    >
    > That's funny, I make your method 25x slower than my char[] one :)


    That likely includes library loading time, in which case it isn't a fair
    comparison. In fact, this is faster if those differences are eliminated.

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Oct 17, 2003
    #7
  8. Andrew Smith

    Paul Lutus Guest

    Michiel Konstapel wrote:

    < snip >

    > If you know the maximum length of s beforehand, this would work:
    > String spaces = " "; // MAX_LENGTH spaces
    > t = spaces.substring(0, s.length()); // this may be off by one, I'm
    > never sure


    Well, yes, but it's easy enough to test for exceeding the length of the
    source string.

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Oct 17, 2003
    #8
  9. On Fri, 17 Oct 2003 20:16:22 +0100, VisionSet wrote:

    >
    > "Paul Lutus" <> wrote in message
    > news:...
    >> This
    >> is much faster:
    >>
    >> String original = "This is my test string.";
    >> String replacement = original.replaceAll("."," ");
    >>
    >> System.out.println("[" + original + "]");
    >> System.out.println("[" + replacement + "]");
    >> System.out.println(original.length() - replecement.length());
    >>
    >> Result:
    >>
    >> [This is my test string.]
    >> [ ]
    >> 0
    >>
    >> Before the purists reply, remember -- the replaceAll() method is written

    > in
    >> native code, and "faster" was the criterion.
    >>

    >
    > That's funny, I make your method 25x slower than my char[] one :)


    That's strange. Because String is immutable, this:
    str += " ";
    has to be expanded to this:
    str = newStringBuffer(str).append(" ").toString();
    which creates two new objects every time round the loop.

    Maybe the optimiser has figured out the redundant moves, moved a single
    StringBuffer construction outside the loop (before) and a single
    toString() outside the loop (after) and just left the append(" ") inside
    the loop. That would explain the good performance, but it must be a pretty
    clever optimiser. People do traditionally avoid string appends inside a
    loop because of the poor preformance they usually get.

    Steve.
     
    Steve Horsley, Oct 17, 2003
    #9
  10. Andrew Smith

    Paul Lutus Guest

    Steve Horsley wrote:

    < snip >

    >> That's funny, I make your method 25x slower than my char[] one :)

    >
    > That's strange. Because String is immutable, this:
    > str += " ";
    > has to be expanded to this:
    > str = newStringBuffer(str).append(" ").toString();
    > which creates two new objects every time round the loop.


    But that wasn't the approach he posted. He declared a char array of the
    correct length, then filled it with spaces using a loop. It's entirely
    reasonable.

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Oct 17, 2003
    #10
  11. Andrew Smith

    Paul Lutus Guest

    Paul Lutus wrote:

    < snip >

    >> That's funny, I make your method 25x slower than my char[] one :)

    >
    > That likely includes library loading time, in which case it isn't a fair
    > comparison. In fact, this is faster if those differences are eliminated.


    I want to retract this. I now think your approach is faster. Mine is only
    faster to enter in the first place, after that, it's slower.

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Oct 17, 2003
    #11
  12. Andrew Smith

    Chris Smith Guest

    VisionSet wrote:
    > "Paul Lutus" <> wrote ...
    > > Before the purists reply, remember -- the replaceAll() method is written

    > in
    > > native code, and "faster" was the criterion.
    > >

    >
    > That's funny, I make your method 25x slower than my char[] one :)


    Indeed. I'm sure the regular expression implementation in Java is quite
    fast for an implementation of generic regular expression searching and
    replacing. However, when the task is far simpler, regular expressions
    aren't going to be the fastest answer.

    Not sure what native code has to do with anything, either.

    --
    www.designacourse.com
    The Easiest Way to Train Anyone... Anywhere.

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Oct 17, 2003
    #12
  13. Andrew Smith

    Paul Lutus Guest

    Chris Smith wrote:

    > VisionSet wrote:
    >> "Paul Lutus" <> wrote ...
    >> > Before the purists reply, remember -- the replaceAll() method is
    >> > written

    >> in
    >> > native code, and "faster" was the criterion.
    >> >

    >>
    >> That's funny, I make your method 25x slower than my char[] one :)

    >
    > Indeed. I'm sure the regular expression implementation in Java is quite
    > fast for an implementation of generic regular expression searching and
    > replacing. However, when the task is far simpler, regular expressions
    > aren't going to be the fastest answer.


    I agree. They are only faster to type in, not faster to execute. I was wrong
    earlier (and retracted).

    > Not sure what native code has to do with anything, either.


    The regexp engine is written in native code, so they (Java regexp
    expressions) may at times compete with similar Perl code for speed. But in
    this case, I am reasonably sure the alternative posted method is way
    faster.

    --
    Paul Lutus
    http://www.arachnoid.com
     
    Paul Lutus, Oct 17, 2003
    #13
  14. VisionSet wrote:

    > char[] spaces = new char[s.length()];
    >
    > for(int i = 0 ; i<spaces.length; i++)
    > spaces = ' ';


    java.util.Arrays.fill() might be better here.
     
    Michael Borgwardt, Oct 17, 2003
    #14
  15. Andrew Smith

    VisionSet Guest

    "Michael Borgwardt" <> wrote in message
    news:bmpn4d$pkpov$-berlin.de...
    > VisionSet wrote:
    >
    > > char[] spaces = new char[s.length()];
    > >
    > > for(int i = 0 ; i<spaces.length; i++)
    > > spaces = ' ';

    >
    > java.util.Arrays.fill() might be better here.
    >


    No it does it the same way, except it goes via fill(char[] a, int fromIndex,
    int toIndex, char val) and hence throws in the usual public method parameter
    checks, so if anything slightly longer.

    --
    Mike W
     
    VisionSet, Oct 17, 2003
    #15
  16. Andrew Smith

    Jon Skeet Guest

    Paul Lutus <> wrote:
    > > Not sure what native code has to do with anything, either.

    >
    > The regexp engine is written in native code, so they (Java regexp
    > expressions) may at times compete with similar Perl code for speed. But in
    > this case, I am reasonably sure the alternative posted method is way
    > faster.


    Which bits are written in native code? Looking in the src.zip which
    comes with JDK1.4.2, the Pattern and Matcher classes don't contain any
    native method declarations as far as I can see. Where is the actual
    native code?

    (This being a lazy Saturday afternoon, I haven't gone *too* far into
    looking, but it's normally fairly easy to spot.)

    --
    Jon Skeet - <>
    http://www.pobox.com/~skeet
    If replying to the group, please do not mail me too
     
    Jon Skeet, Oct 18, 2003
    #16
  17. Andrew Smith

    VisionSet Guest

    "Jon Skeet" <> wrote in message
    news:MPG.19fb6638de8e6379896b9@10.1.1.14...
    > Paul Lutus <> wrote:
    > > > Not sure what native code has to do with anything, either.

    > >
    > > The regexp engine is written in native code, so they (Java regexp
    > > expressions) may at times compete with similar Perl code for speed. But

    in
    > > this case, I am reasonably sure the alternative posted method is way
    > > faster.

    >
    > Which bits are written in native code? Looking in the src.zip which
    > comes with JDK1.4.2, the Pattern and Matcher classes don't contain any
    > native method declarations as far as I can see. Where is the actual
    > native code?
    >
    > (This being a lazy Saturday afternoon, I haven't gone *too* far into
    > looking, but it's normally fairly easy to spot.)
    >


    I couldn't find any either, I did a text search for 'native' on the whole
    regex package and zilch.

    --
    Mike W
     
    VisionSet, Oct 18, 2003
    #17
  18. "Paul Lutus" <> wrote in message
    news:...
    > Michiel Konstapel wrote:
    >
    > < snip >
    >
    > > If you know the maximum length of s beforehand, this would work:
    > > String spaces = " "; // MAX_LENGTH spaces
    > > t = spaces.substring(0, s.length()); // this may be off by one, I'm
    > > never sure

    >
    > Well, yes, but it's easy enough to test for exceeding the length of the
    > source string.


    Yup, but I left that as an exercise for the reader :)
    Michiel
     
    Michiel Konstapel, Oct 18, 2003
    #18
  19. Andrew Smith

    skeptic Guest

    Paul Lutus <> wrote in message > > Not sure what native code has to do with anything, either.
    >
    > The **regexp engine is written in native code**, so they (Java regexp


    Sorry, that's complete BS.

    Regards
     
    skeptic, Oct 18, 2003
    #19
  20. Andrew Smith

    Roedy Green Guest

    On 18 Oct 2003 11:27:40 -0700, (skeptic) wrote or
    quoted :

    >> The **regexp engine is written in native code**, so they (Java regexp

    >
    >Sorry, that's complete


    Somebody claimed a while back it was because the just wrote the code
    well and used good algorithms in plain old Java.

    I'm hoping someone will try a regex that generates byte codes on the
    fly for patterns. I wonder how much faster that could be, especially
    if there were a way to let the native compiler at them.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Oct 18, 2003
    #20
    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. Frodo Larik
    Replies:
    1
    Views:
    1,642
    J├╝rgen Exner
    May 30, 2004
  2. tolisss
    Replies:
    1
    Views:
    381
    =?Utf-8?B?dGlta2xpbmc=?=
    Nov 12, 2005
  3. learningjava
    Replies:
    6
    Views:
    4,377
    learningjava
    Dec 15, 2003
  4. John B. Matthews
    Replies:
    4
    Views:
    695
    John B. Matthews
    Sep 12, 2008
  5. Roedy Green
    Replies:
    3
    Views:
    658
Loading...

Share This Page