• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Performance such as thirdIndexOf(' ');

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
author
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks you Petter
reply
    Bookmark Topic Watch Topic
  • New Topic