Created
June 13, 2012 10:33
-
-
Save banthar/2923321 to your computer and use it in GitHub Desktop.
java split benchmark
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
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.Iterator; | |
import java.util.LinkedList; | |
import java.util.StringTokenizer; | |
public class Split { | |
private static String string = "a/b/c/d/e/f/g/h/i/j/asd/asdas/dasdjasodjoa/sjd/oajs/djoasjd/as/odj/jaowdj/oajw/odj/aojwd/oja/owjd/oja/wjdoja/wdj/awjdojaw/odj/oawjd/oja/wjdoawjdojaw/d/dff"; | |
public static String[] split_tskuzzy(String s, char delimeter) { | |
char[] c = s.toCharArray(); | |
LinkedList<String> ll = new LinkedList<String>(); | |
int index = 0; | |
for (int i = 0; i < c.length; i++) { | |
if (c[i] == delimeter) { | |
ll.add(s.substring(index, i)); | |
index = i + 1; | |
} | |
} | |
String[] arr = new String[ll.size()]; | |
Iterator<String> iter = ll.iterator(); | |
index = 0; | |
for (index = 0; iter.hasNext(); index++) | |
arr[index] = iter.next(); | |
return arr; | |
} | |
public static String[] split_tskuzzy2(String s, char delimeter) { | |
ArrayList<String> ll = new ArrayList<String>(); | |
int index = 0; | |
for (int i = 0; i < s.length(); i++) { | |
if (s.charAt(i) == delimeter) { | |
ll.add(s.substring(index, i)); | |
index = i + 1; | |
} | |
} | |
if(index!=s.length()) | |
ll.add(s.substring(index, s.length())); | |
return ll.toArray(new String[ll.size()]); | |
} | |
public static String[] split_banthar(String s, char delimeter) { | |
int count = 1; | |
for (int i = 0; i < s.length(); i++) | |
if (s.charAt(i) == delimeter) | |
count++; | |
String[] array = new String[count]; | |
int a = -1; | |
int b = 0; | |
for (int i = 0; i < count; i++) { | |
while (b < s.length() && s.charAt(b) != delimeter) | |
b++; | |
array[i] = s.substring(a+1, b); | |
a = b; | |
b++; | |
} | |
return array; | |
} | |
public static void main(String[] args) { | |
int n=1000000; | |
long start; | |
start = System.currentTimeMillis(); | |
for (int i = 0; i < n; i++) | |
split_banthar(string, '/'); | |
System.out.println("split_banthar: " + (System.currentTimeMillis() - start)); | |
start = System.currentTimeMillis(); | |
for (int i = 0; i < n; i++) | |
split_tskuzzy(string, '/'); | |
System.out.println("split_tskuzzy: " + (System.currentTimeMillis() - start)); | |
start = System.currentTimeMillis(); | |
for (int i = 0; i < n; i++) | |
split_tskuzzy2(string, '/'); | |
System.out.println("split_tskuzzy2: " + (System.currentTimeMillis() - start)); | |
start = System.currentTimeMillis(); | |
for (int i = 0; i < n; i++) | |
string.split("/"); | |
System.out.println("string.split: " + (System.currentTimeMillis() - start)); | |
start = System.currentTimeMillis(); | |
for (int i = 0; i < n; i++) { | |
StringTokenizer s = new StringTokenizer(string, "/"); | |
while (s.hasMoreElements()) | |
s.nextElement(); | |
} | |
System.out.println("StringTokenizer: " + (System.currentTimeMillis() - start)); | |
} | |
} |
At least the split_banthar (tested with copy/paste code) does NOT have the same behaviour has the JAVA SPLIT...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was my fastest attempt :(