permaculture playing cards*
The moose likes Java in General and the fly likes Performance  such as thirdIndexOf(' '); Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Performance  such as thirdIndexOf( Watch "Performance  such as thirdIndexOf( New topic
Author

Performance such as thirdIndexOf(' ');

Shay Gaghe
Ranch Hand

Joined: Sep 03, 2001
Posts: 102
Hi
I have s String:
String Text = "JavaRanch is best ";
I need to perform a warping performance ,therefore I determine the amount of spaces within the String , I take the 3 words and concatenates them as the following code:


Its not efficient to do it in this way because its will take lots of processing time. is there a why can I tell the program to cut the string from the third occurrence of the space character?
(Code please)
Thanks in advance
Shay Gaghe
kim jungil
Greenhorn

Joined: Sep 27, 2001
Posts: 27
//how about this one
import java.lang.*;
public class Test {
public static void main(String[] args) {
String s = " al al fdl ";
String temp = s;
int i = 1, n = -1;
while (i != 0) {
i = temp.indexOf(" ") + 1;
temp = temp.substring(i);
n++;
}
System.out.println(n);
}
}

[This message has been edited by kim jungil (edited October 15, 2001).]
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
In fact, both bits of code above have problems.
Shay's code is very clear and straightforward, but he appends words to a String - if you'd replace firstLine by a StringBuffer, performance would be a lot better.
Kim's code tries to optimise away the StringTokenizer at the expense of readability; never a good tradeoff unless you are absolutely sure that it is necessary (sure as in "profiler"). Moreover, in "temp = temp.substring(i);" it tends to create lots of strings - potentially very large strings if "s" may contain a whole document.
What about something likeThis hasn't been tested, so don't expect it even to compile - debug and adapt as needed . Performance should be okay; there's room for improvement but not without affecting code legibility.
- Peter
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Maybe I should clarify a bit: one of the most important concerns in writing Java applications is object churn - flooding the garbage collector with lots of little objects is a surefire way to keep performance low (generational GCs notwithstanding).
One of the most common culprits is string concatenation - if you type "string1 += string2" this actually compiles to "string1 = new StringBuffer(string1).append(string2).toString()". The intermediary StringBuffer has to be garbage collected. If you are appending a lot of small bits one at a time, you're better off creating your own StringBuffer at the start, append to it what you want, and only convert it to a String at the very end.
- Peter
Shay Gaghe
Ranch Hand

Joined: Sep 03, 2001
Posts: 102
Thanks you Petter
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Performance such as thirdIndexOf(' ');
 
Similar Threads
reading one word out of a string
readLine(byte b[], int off, int len) method of Servlet InputStream returns -1
inner class or new class or new method?
URGENT!!! InputStreamReader
2D array: Find an array of characters