import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;
public class Test {
final static int LOOP_COUNT = 1000;
final static String STR_DATA = "abcdefghijklmnopqrstuvwxyz";
static Set<String> set;
public static void main(String[] args) {
warmUp();
set = new HashSet<String>();
showUsedTime();
set = new LinkedHashSet<String>();
showUsedTime();
set = new TreeSet<String>();
showUsedTime();
}
private static void warmUp() {
set = new HashSet<String>();
for (int i = 0; i < LOOP_COUNT; i++) {
set.add(STR_DATA + i);
}
}
private static void showUsedTime() {
String dummy = null;
long startTime = System.nanoTime();
for (int i = 0; i < LOOP_COUNT; i++) {
set.add(STR_DATA + i);
}
long endTime = System.nanoTime();
double elaspedTime = (endTime - startTime) / 1000000.0;
System.out.println("Add Time: " + elaspedTime);
startTime = System.nanoTime();
Iterator<String> iterator = set.iterator();
while (iterator.hasNext()) {
dummy = iterator.next();
}
endTime = System.nanoTime();
elaspedTime = (endTime - startTime) / 1000000.0;
System.out.println("Get Time: " + elaspedTime);
}
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.