-
-
Save jskorpan/1056060 to your computer and use it in GitHub Desktop.
Ultra fast Java string tokenizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class StringTokenizer { | |
private static ThreadLocal<String[]> tempArray = new ThreadLocal<String[]>(); | |
public static String[] tokenize(String string, char delimiter) | |
{ | |
String[] temp = tempArray.get(); | |
int tempLength = (string.length() / 2) + 2; | |
if (temp == null || temp.length < tempLength) | |
{ | |
temp = new String[tempLength]; | |
tempArray.set(temp); | |
} | |
int wordCount = 0; | |
int i = 0; | |
int j = string.indexOf(delimiter); | |
while (j >= 0) | |
{ | |
temp[wordCount++] = string.substring(i, j); | |
i = j + 1; | |
j = string.indexOf(delimiter, i); | |
} | |
temp[wordCount++] = string.substring(i); | |
String[] result = new String[wordCount]; | |
System.arraycopy(temp, 0, result, 0, wordCount); | |
return result; | |
} | |
} |
Author
jskorpan
commented
May 24, 2012
via email
Reusing an object is faster. I can't recall if we actually proved it how
ever
//JT
2012/5/24 Yossale <
[email protected]
Why did you decide to use a ThreadLocal class variable for the tempArray
instead of declaring it inside the function?
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1056060
##
##
Jonas Tärnström
Product Manager
• e-mail: [email protected]
• skype: full name "Jonas Tärnström"
• phone: +46 (0)734 231 552
ESN Social Software AB
www.esn.me
Can you provide a version that supports multiple delim chars?
why/how is this ultra fast?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment