1

I am trying to solve an algorithmic task where speed is of primary importance. In the algorithm, I am using a DFS search in a graph and in every step, I add a char and a String. I am not sure whether this is the bottleneck of my algorithm (probably not) but I am curious what is the fastest and most efficient way to do this.

At the moment, I use this:

transPred.symbol + word

I think that there is might be a better alternative than the "+" operator but most String methods only work with other Strings (would converting my char into String and using one of them make a difference?).

Thanks for answers.

EDIT:

for (Transition transPred : state.transtitionsPred) {
            walk(someParameters, transPred.symbol + word);
        }

transPred.symbol is a char and word is a string

Smajl
  • 7,555
  • 29
  • 108
  • 179
  • Why not just use a StringBuilder or a StringBugger? They're designed for this sort of issue to allow efficient string construction in loops. – GordonM Dec 17 '13 at 09:21
  • And by StringBugger I of course mean StringBuffer. FML. – GordonM Dec 17 '13 at 09:32

2 Answers2

1

A very common problem / concern.

Bear in mind that each String in java is immutable. Thus, if you modify the string it actually creates a new object. This results in one new object for each concatenation you're doing above. This isn't great, as it's simply creating garbage that will have to be collected at some point.

If your graph is overly large, this might be during your traversal logic - and it may slow down your algorithm.

To avoid creating a new String for each concatenation, use the StringBuilder. You can declare one outside your loop and then append each character with StringBuilder.append(char). This does not incur a new object creation for each append() operation.

After your loop you can use StringBuilder.toString(), this will create a new object (the String) but it will only be one for your entire loop.

keyser
  • 18,829
  • 16
  • 59
  • 101
jwa
  • 3,239
  • 2
  • 23
  • 54
  • This only helps if you are repeatedly adding to the same string. If you are producing different strings each time, it is of no benefit because the Java compiler does this for you anyway. – Robin Green Nov 29 '13 at 23:02
  • I need to append a symbol to the beginning of the String but I do not know, which symbol it will be so the resulting strings are always different - i will edit my question – Smajl Nov 29 '13 at 23:05
  • I edited my question... perhaps it is clearer now.. I am looking for time efficient way how to append these two things – Smajl Nov 29 '13 at 23:07
  • 1
    @Smaji - If you're only adding one symbol at a time you could still use append, and then use `StringBuilder.reverse()` at the end, before the `StringBuilder.toString()`, which will invert the order of the characters. – jwa Nov 29 '13 at 23:08
  • He seems to need different strings at each iteration – keyser Nov 29 '13 at 23:10
  • Simple "no" , cant optimize any more is also an answer, I was just curious :-) – Smajl Nov 29 '13 at 23:11
1

Since you replace one char in the string at each iteration I don't think that there is anything faster than a simple + append operation. As mentioned, Strings are immutable, so when you append a char to it, you will get a new String object, but this seems to be unavoidable in your case since you need a new string at each iteration.

If you really want to optimize this part, consider using something mutable like an array of chars. This would allow you to replace the first character without any excessive object creation.

Also, I think you're right when you say that this probably isn't your bottleneck. And remember that premature optimization is the root of all evil etc. (Don't mind the irony that the most popular example of good optimization is avoiding excessive string concatenation).

Community
  • 1
  • 1
keyser
  • 18,829
  • 16
  • 59
  • 101
  • would using some kind of list of chars instead of string be faster? – Smajl Nov 29 '13 at 23:16
  • That would allow you to reuse the same element, so it's definitely an option. If you need to convert it to a String at some point it probably won't make much of a difference though. – keyser Nov 29 '13 at 23:17
  • @Smajl I added some text about it – keyser Nov 29 '13 at 23:25