Skip to content

Instantly share code, notes, and snippets.

@gihara gihara/Test.Java
Created Jan 20, 2016

Embed
What would you like to do?
ArrayList::addとLinkedList::addの速度比較
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class Test {
public static void main(String[] args) {
Test test = new Test();
test.start();
}
private void start() {
for(int i = 1; i < 15; i++) {
test(i);
}
}
private void test(int i) {
System.out.println("====================================================================================================");
long startTime = System.nanoTime();
testArrayListAdd();
System.out.printf("%2d回目 ArrayListへのAdd速度 :%10d \n",i,System.nanoTime() - startTime);
startTime = System.nanoTime();
testLinkedListAdd();
System.out.printf("%2d回目 LinkedListへのAdd速度 :%10d \n",i,System.nanoTime() - startTime);
System.out.println("====================================================================================================");
}
private void testArrayListAdd() {
List<String> list = new ArrayList<>();
for (int i = 0; i < 10000; i++) {
list.add("test");
}
}
private void testLinkedListAdd() {
List<String> list = new LinkedList<>();
for (int i = 0; i < 10000; i++) {
list.add("test");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.