Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@gihara
Created January 20, 2016 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gihara/f0e1a92bf6ac4e027ecc to your computer and use it in GitHub Desktop.
Save gihara/f0e1a92bf6ac4e027ecc to your computer and use it in GitHub Desktop.
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