Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ArrayListとLinkedListのいろいろなパターンでの速度比較
package com.kamatama41.sandbox4j;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
public class ArrayListCompareToLinkedList {
public static void main(String[] args) throws Exception {
final List<Integer> COUNTS = Arrays.asList(1000,10000,100000);
final int EXECUTE_COUNTS = 5;
all(LinkedList.class, COUNTS, EXECUTE_COUNTS);
all(ArrayList.class, COUNTS, EXECUTE_COUNTS);
}
private static <T extends List<?>> void all(Class<T> clazz, List<Integer> COUNTS, int EXECUTE_COUNTS) throws Exception {
System.out.println("--------- " + clazz.getSimpleName() + " ----------");
for (Integer count : COUNTS) {
// ランダムを実行する回数
int randomCount = 3000;
System.out.println("ListSize:" + count + ", RandomCount:" + randomCount);
// オペレーション群の初期化
OperationTimeCounter[] countors = new OperationTimeCounter[]{
new OperationTimeCounter(addSequence),
new OperationTimeCounter(iterator),
new OperationTimeCounter(indexOf),
new OperationTimeCounter(lastIndexOf),
new OperationTimeCounter(addRandom),
new OperationTimeCounter(getRandom),
new OperationTimeCounter(removeRandom),
new OperationTimeCounter(removeSequence)
};
// オペレーション実行
for (int i = 0; i < EXECUTE_COUNTS; i++) {
@SuppressWarnings("unchecked")
List<Integer> list = (List<Integer>) clazz.newInstance();
for (OperationTimeCounter counter : countors) {
counter.execute(list, count, randomCount);
}
}
// アベレージを出力
for (OperationTimeCounter counter : countors) {
System.out.println(counter.operator + ":" + counter.getAverage());
}
}
}
private static class OperationTimeCounter {
Operator operator;
long totalNanoTime = 0;
int executeCount = 0;
OperationTimeCounter (Operator operator) {
this.operator = operator;
}
void execute(List<Integer> list, Integer count, int randomCount) {
totalNanoTime += operator.execute(list, count, randomCount);
executeCount++;
}
long getAverage() {
return totalNanoTime / executeCount;
}
}
private static interface Operator {
long execute(List<Integer> list, Integer count, int randomCount);
}
/** 要素を順に追加する */
private static final Operator addSequence = new Operator() {
@Override
public long execute(List<Integer> list, Integer count, int randomCount) {
long start = System.nanoTime();
for (int i = 0; i < count; i++) {
list.add(i);
}
return System.nanoTime() -start;
}
@Override
public String toString() { return "[addSequence]"; }
};
/** iteratorを使った展開を行う */
private static final Operator iterator = new Operator() {
@Override
public long execute(List<Integer> list, Integer count, int randomCount) {
Iterator<Integer> iterator = list.iterator();
long sum = 0;
long start = System.nanoTime();
while(iterator.hasNext()) {
sum += iterator.next();
}
System.out.println("Sum=" + sum);
return System.nanoTime() -start;
}
@Override
public String toString() { return "[iterator]"; }
};
/** 適当な数値に対してindexOfをかける */
private static final Operator indexOf = new Operator() {
@Override
public long execute(List<Integer> list, Integer count, int randomCount) {
Random random = new Random();
long start = System.nanoTime();
for (int i = 0; i < count; i++) {
list.indexOf(random.nextInt(list.size()));
}
return System.nanoTime() -start;
}
@Override
public String toString() { return "[indexOf]"; }
};
/** 適当な数値に対してindexOfをかける */
private static final Operator lastIndexOf = new Operator() {
@Override
public long execute(List<Integer> list, Integer count, int randomCount) {
Random random = new Random();
long start = System.nanoTime();
for (int i = 0; i < count; i++) {
list.lastIndexOf(random.nextInt(list.size()));
}
return System.nanoTime() -start;
}
@Override
public String toString() { return "[lastIndexOf]"; }
};
/** 要素をランダムに追加する */
private static final Operator addRandom = new Operator() {
@Override
public long execute(List<Integer> list, Integer count, int randomCount) {
Random random = new Random();
long start = System.nanoTime();
for (int i = 0; i < randomCount; i++) {
list.add(random.nextInt(count), i);
}
return System.nanoTime() -start;
}
@Override
public String toString() { return "[addRandom]"; }
};
/** 要素をランダムに検索する */
private static final Operator getRandom = new Operator() {
@Override
public long execute(List<Integer> list, Integer count, int randomCount) {
Random random = new Random();
long start = System.nanoTime();
for (int i = 0; i < randomCount; i++) {
list.get(random.nextInt(count));
}
return System.nanoTime() -start;
}
@Override
public String toString() { return "[getRandom]"; }
};
/** 要素をランダムに削除する */
private static final Operator removeRandom = new Operator() {
@Override
public long execute(List<Integer> list, Integer count, int randomCount) {
Random random = new Random();
long start = System.nanoTime();
for (int i = 0; i < randomCount; i++) {
list.remove(random.nextInt(count));
}
return System.nanoTime() -start;
}
@Override
public String toString() { return "[removeRandom]"; }
};
/** 全要素を1番目から順番に削除する */
private static final Operator removeSequence = new Operator() {
@Override
public long execute(List<Integer> list, Integer count, int randomCount) {
long start = System.nanoTime();
int size = list.size();
for (int i = 0; i < size; i++) {
list.remove(0);
}
return System.nanoTime() -start;
}
@Override
public String toString() { return "[removeSequence]"; }
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment