import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
public class Test {
final static int LOOP_COUNT = 1000;
static List<Integer> list;
public static void main(String[] args) {
warmUp();
list = new ArrayList<Integer>();
System.out.println("===== ArrayList =====");
showUsedTime();
list = new Vector<Integer>();
System.out.println("===== Vector =====");
showUsedTime();
list = new LinkedList<Integer>();
System.out.println("===== LinkedList =====");
showLinkedListUsedTime();
}
private static void warmUp() {
list = new ArrayList<Integer>();
for (int i = 0; i < LOOP_COUNT; i++) {
list.add(i);
}
}
private static void showLinkedListUsedTime() {
int dummy = 0;
showUsedTime();
long startTime = System.nanoTime();
for (int i = 0; i < LOOP_COUNT; i++) {
dummy = ((LinkedList<Integer>) list).peek();
}
long endTime = System.nanoTime();
double elaspedTime = (endTime - startTime) / 1000000.0;
System.out.println("Peek Time: " + elaspedTime);
}
private static void showUsedTime() {
int dummy = 0;
long startTime = System.nanoTime();
for (int i = 0; i < LOOP_COUNT; i++) {
list.add(i);
}
long endTime = System.nanoTime();
double elaspedTime = (endTime - startTime) / 1000000.0;
System.out.println("Add Time: " + elaspedTime);
startTime = System.nanoTime();
for (int i = 0; i < LOOP_COUNT; i++) {
dummy = list.get(i);
}
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.