String consisting of spaces

A

Andrew Smith

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 += " ";
}
 
V

VisionSet

Andrew Smith said:
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+"<");
 
G

Guest

Andrew Smith said:
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.
 
P

Paul Lutus

Andrew said:
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.
 
M

Michiel Konstapel

Andrew Smith said:
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
 
V

VisionSet

Paul Lutus said:
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 :)
 
P

Paul Lutus

VisionSet said:
Paul Lutus said:
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.
 
P

Paul Lutus

Michiel Konstapel wrote:

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.
 
S

Steve Horsley

Paul Lutus said:
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.
 
P

Paul Lutus

Steve Horsley wrote:

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.
 
P

Paul Lutus

Paul Lutus wrote:

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.
 
C

Chris Smith

VisionSet said:
Paul Lutus said:
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
 
P

Paul Lutus

Chris said:
VisionSet said:
Paul Lutus said:
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.
 
M

Michael Borgwardt

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

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


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

VisionSet

Michael Borgwardt said:
VisionSet said:
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.
 
J

Jon Skeet

Paul Lutus said:
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.)
 
V

VisionSet

Jon Skeet said:
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.
 
M

Michiel Konstapel

Paul Lutus said:
Michiel Konstapel wrote:



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
 
R

Roedy Green

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top