Java String.split() vs. Pattern.split() | Bar54
split() For example: String myString = "Hy my name is bob"; String[] words = myString.split(" "); If you need to perform the same kind of split frequently, the Java Regular Expressions API provides a good oportunity for a performance improvement. The API provides a Pattern class to precompile a split pattern and execute it on a string. The example above would look like: Pattern p = Pattern.compile(" "); String myString = "Hy my name is bob"; String[] words = p.split(myString); Now, why would this lead to performance improvement? Internally, String.split() is implemented as public String[] split(String regex, int limit) { return Pattern.compile(regex).split(this, limit); } As you can see, String.split() instantiates a new Pattern object on the fly. The garbage collector can remove it quickly, but if you perform the same split again and again, this requires a reasonable overhead. So what you can do is: compile your Pattern in advance.Read full article from Java String.split() vs. Pattern.split() | Bar54
No comments:
Post a Comment