Created
April 30, 2012 07:12
-
-
Save fehmicansaglam/2556158 to your computer and use it in GitHub Desktop.
Test unsychronized Set implementations
This file contains hidden or 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
private static final int COUNT = 1000000; | |
private static void testAdd(Set<String> set) { | |
final long start = System.nanoTime(); | |
for (int i = 0; i < COUNT; ++i) { | |
set.add("test" + Math.random()); | |
} | |
final long elapsed = System.nanoTime() - start; | |
System.out.println("testAdd: " + elapsed / 1000); | |
} | |
private static void testRemove(Set<String> set) { | |
final long start = System.nanoTime(); | |
for (int i = 0; i < COUNT; ++i) { | |
set.remove("test" + Math.random()); | |
} | |
final long elapsed = System.nanoTime() - start; | |
System.out.println("testRemove: " + elapsed / 1000); | |
} | |
private static void testContains(Set<String> set) { | |
final long start = System.nanoTime(); | |
for (int i = 0; i < COUNT; ++i) { | |
set.contains("test" + Math.random()); | |
} | |
final long elapsed = System.nanoTime() - start; | |
System.out.println("testContains: " + elapsed / 1000); | |
} | |
private static void testSize(Set<String> set) { | |
final long start = System.nanoTime(); | |
for (int i = 0; i < COUNT; ++i) { | |
set.size(); | |
} | |
final long elapsed = System.nanoTime() - start; | |
System.out.println("testSize: " + elapsed / 1000); | |
} | |
private static void testIterator(Set<String> set) { | |
final long start = System.nanoTime(); | |
for (String item : set) { | |
set.size(); | |
} | |
final long elapsed = System.nanoTime() - start; | |
System.out.println("testIterator: " + elapsed / 1000); | |
} | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) { | |
//Some unnecessary ops | |
for (int i = 0; i < COUNT; ++i) { | |
Math.random(); | |
} | |
Set<String> set = new HashSet<String>(); | |
System.out.println("**************************"); | |
System.out.println("Test HashSet"); | |
testAdd(set); | |
testContains(set); | |
testSize(set); | |
testIterator(set); | |
testRemove(set); | |
set = new LinkedHashSet<String>(); | |
System.out.println("**************************"); | |
System.out.println("Test LinkedHashSet"); | |
testAdd(set); | |
testContains(set); | |
testSize(set); | |
testIterator(set); | |
testRemove(set); | |
set = new TreeSet<String>(); | |
System.out.println("**************************"); | |
System.out.println("Test TreeSet"); | |
testAdd(set); | |
testContains(set); | |
testSize(set); | |
testIterator(set); | |
testRemove(set); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment