Java分裂字符串表现 [英] 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.
我还了解到 split
实际上需要一个正则表达式,这对我来说没用。
I also learned that split
actually takes a regular expression, which is useless for me here.
所以我的问题是,我可以使用哪种替代方法来优化字符串拆分?我见过 StringUtils.split
但是它更快吗?
So my question is, what alternative can I use in order to optimize the string splitting? I've seen StringUtils.split
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)
推荐答案
String.split(String)
赢了如果您的模式只有一个字符长,则不要创建正则表达式。当按单个字符拆分时,它将使用非常有效的专用代码。在这种特殊情况下, StringTokenizer
的速度并不快。
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.
这是在OpenJDK7 / OracleJDK7中引入的。 这是一个错误报告和提交。我在这里制作了简单基准。
This was introduced in OpenJDK7/OracleJDK7. Here's a bug report and a commit. 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
这篇关于Java分裂字符串表现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!