Matthieu Napoli Matthieu Napoli - 1 year ago 101
Java Question

Java split String performances

Here is the current code in my application:

String[] ids = str.split("/");

When profiling the application, I noticed that a non negligeable time is spent for splitting the string.

I also learned that
actually takes a regular expression, which is useless for me here.

So my question is, what alternative can I use in order to optimize the string splitting? I've seen
but is it faster?

(I would've tried and tested myself but profiling my application takes a lot of time, so if someone already knows the answer that's some time saved)

Answer Source

String.split(String) won't create regexp if your pattern is only one character long. When splitting by single character, it will use specialized code which is pretty efficient. StringTokenizer is not much faster in this particular case.

Here's a bug report and a commit. This wasn't that long ago so it won't affect you, if you are using older versions (probably anything other than oracle7 or openjdk7). I've made a simple benchmark here.

$ java -version
java version "1.8.0_20"
Java(TM) SE Runtime Environment (build 1.8.0_20-b26)
Java HotSpot(TM) 64-Bit Server VM (build 25.20-b23, mixed mode)

$ java Split
split_banthar: 1231
split_tskuzzy: 1464
split_tskuzzy2: 1742
string.split: 1291
StringTokenizer: 1517
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download