Created
January 20, 2016 13:10
-
-
Save gihara/f0e1a92bf6ac4e027ecc to your computer and use it in GitHub Desktop.
ArrayList::addとLinkedList::addの速度比較
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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