Created
September 28, 2012 12:51
-
-
Save bxt/3799628 to your computer and use it in GitHub Desktop.
Test Java collection performance against set operations with various input characteristics
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.Collection; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.TreeSet; | |
public class ListSetTest { | |
public static void main(String[] args) { | |
int opcount = 900000; | |
Params[] paramss = new Params[]{ | |
new Params(1, opcount, 4, ""), | |
new Params(5, opcount, 4, ""), | |
new Params(500, opcount, 4, ""), | |
new Params(50, opcount, 4, "dfskgjhsdfkghsdklfghsdlkfghsdlkfghsdlkfghsdlfkh"), | |
new Params(50, opcount, 4, new String(new char[100]).replace("\0", "aloha")), | |
new Params(50, opcount, 2, ""), | |
new Params(50, opcount, 20, ""), | |
}; | |
for (Params params : paramss) { | |
System.out.println("\n"+params); | |
int different = params.different; | |
int operations = params.operations; | |
int base = params.base; | |
String pad = params.pad; | |
List<Collection<String>> collections = new ArrayList<Collection<String>>(3); | |
collections.add(new ArrayList<String>()); | |
collections.add(new HashSet<String>()); | |
collections.add(new TreeSet<String>()); | |
String[] strings = new String[different]; | |
for(int i = 0; i < strings.length; i++) { | |
strings[i] = pad + Integer.toString(i, base); | |
} | |
for(Collection<String> collection : collections) { | |
System.out.print(String.format("%25s",collection.getClass().getCanonicalName())+": "); | |
long start = System.nanoTime(); | |
for(int i = 0; i < operations; i++) { | |
String s = strings[i%different]; | |
if(!collection.contains(s)) | |
collection.add(s); | |
} | |
long end = System.nanoTime(); | |
System.out.println(String.format("%,13d", end-start)); | |
if(collection.size() != strings.length) | |
throw new RuntimeException(collection.size()+" vs "+strings.length); | |
} | |
} | |
} | |
private static class Params { | |
private int different = 50; | |
private int operations = 1000; | |
private int base = 4; | |
private String pad = ""; | |
public Params(int different, int operations, int base, String pad) { | |
this.different = different; | |
this.operations = operations; | |
this.base = base; | |
this.pad = pad; | |
} | |
@Override | |
public String toString() { | |
return "Params [different=" + different + ", operations=" | |
+ operations + ", base=" + base + ", pad=" + pad + "]"; | |
} | |
} | |
} |
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
Params [different=1, operations=900000, base=4, pad=] | |
java.util.ArrayList: 44.975.269 | |
java.util.HashSet: 45.535.117 | |
java.util.TreeSet: 60.807.449 | |
Params [different=5, operations=900000, base=4, pad=] | |
java.util.ArrayList: 67.473.101 | |
java.util.HashSet: 57.660.121 | |
java.util.TreeSet: 85.644.684 | |
Params [different=500, operations=900000, base=4, pad=] | |
java.util.ArrayList: 2.850.017.810 | |
java.util.HashSet: 46.634.977 | |
java.util.TreeSet: 256.601.049 | |
Params [different=50, operations=900000, base=4, pad=dfskgjhsdfkghsdklfghsdlkfghsdlkfghsdlkfghsdlfkh] | |
java.util.ArrayList: 1.974.526.372 | |
java.util.HashSet: 48.995.054 | |
java.util.TreeSet: 781.946.030 | |
Params [different=50, operations=900000, base=4, pad=alohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaalohaaloha] | |
java.util.ArrayList: 17.068.582.738 | |
java.util.HashSet: 47.704.387 | |
java.util.TreeSet: 6.492.455.885 | |
Params [different=50, operations=900000, base=2, pad=] | |
java.util.ArrayList: 317.699.875 | |
java.util.HashSet: 42.788.958 | |
java.util.TreeSet: 178.698.410 | |
Params [different=50, operations=900000, base=20, pad=] | |
java.util.ArrayList: 316.145.488 | |
java.util.HashSet: 41.882.418 | |
java.util.TreeSet: 127.896.143 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment