This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Performance such as thirdIndexOf(' ');

 
Shay Gaghe
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//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
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks you Petter
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic