Skip to content

Instantly share code, notes, and snippets.

@banthar
Created June 13, 2012 10:33
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save banthar/2923321 to your computer and use it in GitHub Desktop.
Save banthar/2923321 to your computer and use it in GitHub Desktop.
java split benchmark
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));
}
}
@banthar
Copy link
Author

banthar commented Jun 13, 2012

Results:

$ /opt/jdk1.6.0_32/bin/java Split
split_banthar: 1809
split_tskuzzy: 2964
split_tskuzzy2: 2391
string.split: 4116
StringTokenizer: 2879

$ /opt/jre1.7.0_03/bin/java Split
split_banthar: 1835
split_tskuzzy: 3168
split_tskuzzy2: 2435
string.split: 2569
StringTokenizer: 2748

@iamrukia
Copy link

In Java 7, the string.split is much faster.

@cikavladimir
Copy link

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

@LucasAlfare
Copy link

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

@LucasAlfare
Copy link

LucasAlfare commented Dec 7, 2019

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;
    }

@marcolopes
Copy link

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