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)); | |
} | |
} |
In Java 7, the string.split is much faster.
Java 1.8.0_91 from Eclipse in Win 10 and with i5 processor:
split_banthar: 681
split_tskuzzy: 692
split_tskuzzy2: 764
string.split: 740
StringTokenizer: 757
Results in Java 1.8.0_144 Linux Mint i3 6GB Ram (in sorted order):
split_tskuzzy: 1656
StringTokenizer: 1745
split_tskuzzy2: 1887
string.split: 1980
split_banthar: 2421
This was my fastest attempt :(
public static LinkedList<String> split_lucas(String s, char delimeter){
LinkedList<String> elements = new LinkedList<>();
int lastElementStartIndex = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == delimeter){
elements.add(s.substring(lastElementStartIndex, i));
lastElementStartIndex = i + 1;
}
}
if (s.charAt(s.length() - 1) != delimeter){
elements.add(s.substring(lastElementStartIndex));
}
return elements;
}
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
Results: