๋ชฉ์ฐจ
๋ณต์กํ ๋ก์ง์ ์ํํ๋ ๋ฐฐ์น ํ๋ก๊ทธ๋จ์ด ์๋ค๊ณ ๊ฐ์ ํ๋ค.
๋ก์ง์ ๋งค๊ฐ๋ณ์๋ก ๋์ด์จ ์ธ์๋งํผ ๋ฃจํ๋ฅผ ๋๋ฉด์ list์ ์๋ถ๋ถ์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ๋ ๋ก์ง์ด๋ค.(์๋ถ๋ถ์ธ๊ฒ ์ค์)
๋ฐฐ์นํ๋ก๊ทธ๋จ ์ฝ๋๋ฅผ ์ ์ ๊ฐ๋ฐ์๋ค์ด ๋ฆฌํฉํ ๋ง ํ๋ค๋ ์คํ ๋ฆฌ๋ค.
Ver 1: ArrayList ์ฌ์ฉ์ ์ฑ๋ฅ ๋ฌธ์
package collection.test;
import collection.list.MyArrayList;
public class BatchProcessorV1 {
// MyArrayList์ ์ง์ ์์กด(bad)
private final MyArrayList<Integer> list = new MyArrayList<>();
//์์ฒญ ๋ณต์กํ ๋ก์ง์ด๋ผ๊ณ ๊ฐ์
public void logic(int size) {
long startTime = System.currentTimeMillis();
//์
๋ฌด๋ฅผ ํ๋ค๋ณด๋ ๋ฆฌ์คํธ์ ์๋ถ๋ถ์ ๋ฐ์ดํฐ๋ฅผ ์ฝ์
ํ๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ ๋ฐฐ์ด๋ฆฌ์คํธ๋ณด๋ค Linkedlist๊ฐ ๋ ๋ซ๊ฒ ๋ค๋ ํ๋จ์ด ๋ค์ด์ ์ฝ๋๋ฅผ ๊ณ ์ณ์ผ ๊ฒ ๋ค๊ณ ๋ง์์ ๋จน์
//๊ทธ๋์ ์ฝ๋๋ฅผ ๊ฐ์ ํ๊ฒ ๋๊ณ ๊ทธ๊ฒ BatchProcessorV2๊ฐ ๋จ
for (int i = 0; i < size; i++) {
list.add(0, i);
}
long endTime = System.currentTimeMillis();
System.out.println("ํฌ๊ธฐ: " + size + ", ๊ณ์ฐ์๊ฐ: " + (endTime - startTime) + "ms");
}
}
์ ์ผ ๋จผ์ MyArrayList๋ฅผ ์ฌ์ฉํด์ ๋ฐฐ์นํ๋ก๊ทธ๋จ์ ์คํ์ํจ ์ฝ๋๋ค.
์ด๋ ๊ฒ ์์ฑํ ํ๋ก๊ทธ๋จ์ ์ค์ ๋๋ฆฌ๋ค ๋ณด๋ ์ฑ๋ฅ์ด์๊ฐ ๋ฐ์ํด์ ์๋์ ๊ฐ์ด ๊ฐ์ ์์ผฐ๋ค.
ArrayList๋ ์๋ถ๋ถ์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ๊ธฐ ์ํด์๋ ๊ธฐ์กด ๋ฐ์ดํฐ๋ฅผ ํ์นธ์ฉ ๋ค๋ก ๋ฐ์ด์ผ ๋๋ ์ฐ์ฐ์ด ๋ฐ์ํด์ ๋๋ฆฌ๋ค.
1์ฐจ ๋ฆฌํฉํ ๋ง (Ver 2: LinkedList๋ก ๊ฐ์ )
package collection.test;
import collection.list.MyLinkedList;
public class BatchProcessorV2 {
// LinkedList๋ก ๋ฐ๊ฟ์ ์ฑ๋ฅ์ ๋ง์ด ๊ฐ์ ๋์์
// ํ์ง๋ง ์ด ์ฝ๋๋ ์ฌ์ ํ BatchProcessorํด๋์ค๊ฐ MyLinkedList์ ์ง์ ์ ์ผ๋ก ์์กดํ๊ณ ์์ด์ ๋ณ๊ฒฝ์ฌํญ์ด ์๊ธฐ๋ฉด ์ด ์ฝ๋๋ฅผ ๋ ์์ ํด์ผ ํจ
// ์ด๋ฅผ ๋คํ์ฑ๊ณผ ์ ๋ค๋ฆญ์ ์ด์ฉํด์ ๊ฐ์ ํด๋ณด๊ฒ ์
// ๊ทธ ์ฝ๋๊ฐ BatchProcessorV3์
private final MyLinkedList<Integer> list = new MyLinkedList<>();
//์์ฒญ ๋ณต์กํ ๋ก์ง์ด๋ผ๊ณ ๊ฐ์
public void logic(int size) {
long startTime = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
list.add(0, i);
}
long endTime = System.currentTimeMillis();
System.out.println("ํฌ๊ธฐ: " + size + ", ๊ณ์ฐ์๊ฐ: " + (endTime - startTime) + "ms");
}
}
์ฝ๋๋ฅผ 1์ฐจ ๋ฆฌํฉํ ๋ง ํด์ ์ฑ๋ฅ์ด์๋ ๋ง์ด ์ค์ด๋ค์์ง๋ง ์ฌ์ ํ ๋ฐฐ์น ํ๋ก๊ทธ๋จ์ด LinkedList๋ผ๋ ํด๋์ค๋ฅผ ์ง์ ์์กดํ๊ณ ์์ด์ ๋ณ๊ฒฝ์ฌํญ์ด ์๊ธฐ๋ฉด ๋ฐฐ์น ํ๋ก๊ทธ๋จ์ ์ฝ๋๋ฅผ ๋ ์์ ํด์ผ ๋๋ค.
๊ณ ๋ฏผํ๊ณ ์๋ ์ฐฐ๋ ์ฌ์ผ์ ํ ์๋์ด ๊ฐ๋ฐ์๊ฐ ๋ค์ ์ฝ๋๋ฅผ ๊ฐ์ ํ๊ณ ์ ์ ํ ๋ ๋ฌ๋ค.
2์ฐจ ๋ฆฌํฉํ ๋ง (Ver 3: ์ธํฐํ์ด์ค ๋์ ๊ณผ ์์กด์ฑ ์ฃผ์ )
package collection.test;
import collection.list.MyList;
public class BatchProcessorV3 {
/*
MyArrayList์ MyLinkedList์ ์์ ํ์
์ธ MyList๋ฅผ ๊ฐ์ง๊ณ ์๊ณ (์ถ์์ ์ธ ์ธํฐํ์ด์ค์ ์์กด)
์์ฑ์๋ฅผ ํตํด์ ์ธ๋ถ์์ ํด๋น ์ธ์คํด์ค๋ฅผ ์ฃผ์
๋ฐ์
MyArrayList์ MyLinkedList๋ ๋ ๋ค MyList๋ฅผ ๊ตฌํํ๊ณ ์์ด์ ์๋์ฒ๋ผ MyListํ์
์ผ๋ก ๋ฐ์ ์ ์์
MyList = new MyArrayList;
MyList = new MyLinkedList;
๊ทธ๋ฌ๋ฉด ์ด ์ฝ๋์ ํ๋ ๋ถ๋ถ์ ์ ํ ๊ณ ์น ๊ฒ ์๊ณ ์ฌ์ฉํ๋ ์ชฝ์์ ์์ฑ์์์ MyArrayList๋ MyLinkedList๋ง ๋ง๋ค์ด์ ๋ฃ์ด์ฃผ๋ฉด ๋จ
๋ ์ถ๊ฐ๋ก MyList๋ฅผ ๊ตฌํํ๋ ๋ค๋ฅธ ์๋ฃ๊ตฌ์กฐ๊ฐ ํ์ํ๋ฉด MyList๋ฅผ ๊ตฌํํ ํด๋์ค๋ง ์ถ๊ฐํด์ ์ธ๋ถ์์ ์์ฑ์๋ก ๋ฃ์ด์ฃผ๋ฉด ๋๋๋ค.
์ด ์ฝ๋๋ฅผ ๋ณด๋ ์์ฐ์ค๋ฝ๊ฒ ์คํ๋ง์ ์์ฑ์ ์์กด์ฑ ์ฃผ์
์ด ์ฐ์๋์๋ค.
*/
private final MyList<Integer> list;
public BatchProcessorV3(MyList<Integer> list) {
this.list = list;
}
//์์ฒญ ๋ณต์กํ ๋ก์ง์ด๋ผ๊ณ ๊ฐ์
public void logic(int size) {
long startTime = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
list.add(0, i);
}
long endTime = System.currentTimeMillis();
System.out.println("ํฌ๊ธฐ: " + size + ", ๊ณ์ฐ์๊ฐ: " + (endTime - startTime) + "ms");
}
}
MyList
package collection.list;
public interface MyList<E> {
int size();
void add(E e);
void add(int index, E e);
E get(int index);
E set(int index, E e);
E remove(int index);
int indexOf(E e);
}
MyArrayList
package collection.list;
import java.util.Arrays;
public class MyArrayList<E> implements MyList<E> {
private static final int DEFAULT_CAPACITY = 5;
private Object[] elementData;
private int size = 0;
public MyArrayList() {
elementData = new Object[DEFAULT_CAPACITY];
}
public MyArrayList(int initialCapacity) {
elementData = new Object[initialCapacity];
}
@Override
public int size() {
return size;
}
//...์๋ต
}
MyLinkedList
package collection.list;
public class MyLinkedList<E> implements MyList<E> {
private Node<E> first;
private int size = 0;
@Override
public void add(E e) {
Node<E> newNode = new Node<>(e);
if(first == null) {
first = newNode;
} else {
Node<E> lastNode = getLastNode();
lastNode.next = newNode;
}
size++;
}
//...์๋ต
}
๋ถ๋ชจ ํ์ ์ด ์์ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ ์ ์๋ ๋คํ์ฑ๊ณผ ๋ฐ์ดํฐ ํ์ ์ ์ปดํ์ผ ์์ ์ ์ง์ ํ๋ ์ ๋ค๋ฆญ์ ํ์ฉํจ์ผ๋ก์จ, ์ฝ๋๋ ๋ ์ ์ฐํ๊ณ ํ์ฅ ๊ฐ๋ฅํด์ก์ผ๋ฉฐ, OCP ์์น์ ์ค์ํ ์ ์๊ฒ ๋์๋ค.
์ฌ์ฉํ๋ ์ฝ๋
package collection.test;
import collection.list.MyArrayList;
import collection.list.MyLinkedList;
import collection.list.MyList;
public class BatchProcessorMain {
public static void main(String[] args) {
//MyArrayList๋ฅผ ์ฌ์ฉ, ์์ํ์
์ผ๋ก ๋ฐ์
MyList<Integer> myArrayList = new MyArrayList<>();
BatchProcessorV3 processor1 = new BatchProcessorV3(myArrayList);
//ArrayList๋ ๋ฐ์ดํฐ๋ฅผ ์ฐพ๋ ๊ฑด ๋น ๋ฅด์ง๋ง ๋ฃ๊ณ ๋์ ๋ฐ์ดํฐ๋ฅผ ๋ฏธ๋ ์ฐ์ฐ์ด ํ์ํ๋๊น ๋๋ฆฌ๋ค.
//9๋ง๊ฑด
processor1.logic(90_000); // ๊ฒฐ๊ณผ: 5969ms
//MyLinkedList๋ฅผ ์ฌ์ฉ, ์์ํ์
์ผ๋ก ๋ฐ์
MyList<Integer> myLinkedList = new MyLinkedList<>();
BatchProcessorV3 processor2 = new BatchProcessorV3(myLinkedList);
//LinkedList๋๊น ์์ ๋ฐ์ดํฐ๋ฅผ ๋ฃ๋ ๊ฒฝ์ฐ๋ ์ฐธ์กฐ๊ฐ๋ง ๋ณ๊ฒฝํ๋ฉด ๋๊ณ ์ค์ ๋ฐ์ดํฐ์ ์ ๋ ฌ์ ์์ผ๋๊น ์๋๊ฐ ArrayList์ ๋นํด์ ํจ์ฌ ๋น ๋ฅด๋ค.
//9๋ง๊ฑด
processor2.logic(90_000); // ๊ฒฐ๊ณผ: 3ms
}
}
์ด์ ๋ค๋ฅธ ๋ฆฌ์คํธํํ์ ๋ค๋ฅธ ์๋ฃ๊ตฌ์กฐ๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด MyList๋ฅผ ๊ตฌํํ ํด๋์ค๋ฅผ ๋ง๋ค๊ณ ๋ฉ์๋ ์ค๋ฒ๋ผ์ด๋ํด์ ์ธ๋ถ์์ ์์ฑ์๋ฅผ ํตํด ์ธ์คํด์ค๋ฅผ ๋ฃ์ด์ฃผ๊ธฐ๋ง ํ๋ฉด ๋๋ค.(๋ฐฐ์น ํ๋ก๊ทธ๋จ์ ์ ํ ์์ ํ ํ์๊ฐ ์๋ค.)
๊น์ํ์ ์ค์ ์๋ฐ - ์ค๊ธ 2ํธ ๊ฐ์ | ๊น์ํ - ์ธํ๋ฐ
๊น์ํ | ์๋ฐ ์ ๋ค๋ฆญ๊ณผ ์ปฌ๋ ์ ํ๋ ์์ํฌ๋ฅผ ์ค๋ฌด ์ค์ฌ์ผ๋ก ๊น์ด์๊ฒ ํ์ตํฉ๋๋ค. ์๋ฃ ๊ตฌ์กฐ์ ๋ํ ๊ธฐ๋ณธ๊ธฐ๋ ํจ๊ป ํ์ตํฉ๋๋ค., ๊ตญ๋ด ๊ฐ๋ฐ ๋ถ์ผ ๋์ ์๊ฐ์ 1์, ์ ๋๋ก ๋ง๋ ๊น์ํ์ ์ค์
www.inflearn.com
๊ฐ์ธ ์คํฐ๋ ๊ธฐ๋ก์ ๋ฉ๋ชจํ๋ ๊ณต๊ฐ์ด๋ผ ํ๋ฆฐ์ ์ด ์์ ์ ์์ต๋๋ค.
ํ๋ฆฐ ์ ์์ ๊ฒฝ์ฐ ๋๊ธ ๋ถํ๋๋ฆฝ๋๋ค.
๋๊ธ