U E D R , A S I H C RSS

Java/Capacity Is Changed By DataIO

재미로 보는 Java Container Capacity 변화


더 할만한것

capacity 정보를 제공하는 것이 ~cpp StringBuffer , Vector 밖에 없다. 다른 것들을 볼려면 상속받아서 내부 인자를 봐야 겠다.

결과

capacity 변화가 있을때 마다만 출력
~cpp 

Show String Buffer capactity by Data I/O in increment
data length:         0	capacity:        16
data length:        17	capacity:        34   <-- 현재 길이의 두배로
data length:        35	capacity:        70
data length:        71	capacity:       142
data length:       143	capacity:       286
data length:       287	capacity:       574
data length:       575	capacity:     1,150
data length:     1,151	capacity:     2,302
data length:     2,303	capacity:     4,606
data length:     4,607	capacity:     9,214
data length:     9,215	capacity:    18,430
data length:    18,431	capacity:    36,862
data length:    36,863	capacity:    73,726
data length:    73,727	capacity:   147,454
data length:   147,455	capacity:   294,910
data length:   294,911	capacity:   589,822
data length:   589,823	capacity: 1,179,646
data length: 1,000,000	capacity: 1,179,646  <-- 마지막 출력은 임의이다.

Show String Buffer capactity by Data I/O in decrement
data length: 1,000,000	capacity: 1,179,646
data length:         0	capacity: 1,179,646  <-- 건드리지 않음

Show Vector capactity by Data I/O in increment
data length:         0	capacity:        10
data length:        11	capacity:        20   <-- 이전 capacity 의 두배로
data length:        21	capacity:        40
data length:        41	capacity:        80
data length:        81	capacity:       160
data length:       161	capacity:       320
data length:       321	capacity:       640
data length:       641	capacity:     1,280
data length:     1,281	capacity:     2,560
data length:     2,561	capacity:     5,120
data length:     5,121	capacity:    10,240
data length:    10,241	capacity:    20,480
data length:    20,481	capacity:    40,960
data length:    40,961	capacity:    81,920
data length:    81,921	capacity:   163,840
data length:   163,841	capacity:   327,680
data length:   327,681	capacity:   655,360
data length:   655,361	capacity: 1,310,720
data length: 1,000,000	capacity: 1,310,720 <-- 마지막 출력은 임의이다.

Show String Buffer capactity by Data I/O in decrement
data length: 1,000,000	capacity: 1,310,720
data length:         0	capacity: 1,310,720  <-- 건드리지 않음

소스

이런;; 간단한 소스가 늘어나 버린것 처럼 보인다.

~cpp 
import java.io.PrintStream;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.Vector;

public class CapacityTest {
	private static final int NUMBER_LIMIT_LEN = 10;
	private PrintStream out;
	private PrintWriter k;
	private int totalNumOfData = 1000000;
	
	public static void main(String[] args) {
		CapacityTest capacity = new CapacityTest(System.out);

		capacity.testStringBuffer();
		capacity.testVector();

	}
	public CapacityTest(PrintStream anOut) {
		out = anOut;
		if (out == null)
			throw new NullPointerException("out is null");
	}
	public void testStringBuffer() {
		StringBuffer stringBuffer = new StringBuffer();
		showStringBufferIncrease(stringBuffer);
		showStringBufferDecrease(stringBuffer);
	}
	public void testVector() {
		Vector vector = new Vector();
		showVectorIncrease(vector);
		showVectorDecrease(vector);
	}

	private void printTitle(String aContainerKind, String aSituation) {
		out.println();
		out.println(
			"Show "
				+ aContainerKind
				+ " capactity by Data I/O in "
				+ aSituation);
	}
	private void printContainerState(int aSize, int aCapacity) {
		DecimalFormat df = new DecimalFormat("###,###,###");

		String size = df.format(aSize);
		String capacity = df.format(aCapacity);

		size = getShowedString(size, NUMBER_LIMIT_LEN);
		capacity = getShowedString(capacity, NUMBER_LIMIT_LEN);

		out.println("data length:" + size + "\tcapacity:" + capacity);
	}
	private String getShowedString(String aSrc, int aLimit) {
		int insufficientLen = aLimit - aSrc.length();
		StringBuffer showedString = new StringBuffer(aLimit);
		for (int i = 0; i < insufficientLen; i++)
			showedString.append(" ");
		showedString.append(aSrc);
		return showedString.toString();
	}

	public void showStringBufferIncrease(StringBuffer stringBuffer) {
		printTitle("String Buffer", "increment");
		printContainerState(stringBuffer.length(), stringBuffer.capacity());

		for (int length = 0; length < totalNumOfData; length++) {
			int oldCapacity = stringBuffer.capacity();
			stringBuffer.append((char) ('a' + length % 26));

			if (oldCapacity != stringBuffer.capacity())
				printContainerState(
					stringBuffer.length(),
					stringBuffer.capacity());
		}
		printContainerState(stringBuffer.length(), stringBuffer.capacity());
	}
	public void showStringBufferDecrease(StringBuffer stringBuffer) {

		printTitle("String Buffer", "decrement");
		printContainerState(stringBuffer.length(), stringBuffer.capacity());

		for (int counter = stringBuffer.length(); counter > 0; counter--) {
			int oldCapacity = stringBuffer.capacity();
			stringBuffer.delete(
				stringBuffer.length() - 1,
				stringBuffer.length());

			if (oldCapacity != stringBuffer.capacity())
				printContainerState(
					stringBuffer.length(),
					stringBuffer.capacity());
		}
		printContainerState(stringBuffer.length(), stringBuffer.capacity());
	}
	public void showVectorIncrease(Vector aVector) {
		printTitle("Vector", "increment");
		printContainerState(aVector.size(), aVector.capacity());

		for (int size = 0; size < totalNumOfData; size++) {
			int oldCapacity = aVector.capacity();
			aVector.add("This is Gabage Data");

			if (oldCapacity != aVector.capacity())
				printContainerState(aVector.size(), aVector.capacity());
		}
		printContainerState(aVector.size(), aVector.capacity());
	}
	public void showVectorDecrease(Vector aVector) {
		printTitle("String Buffer", "decrement");
		printContainerState(aVector.size(), aVector.capacity());

		for (int counter = aVector.size(); counter > 0; counter--) {
			int oldCapacity = aVector.capacity();
			aVector.remove(aVector.size() - 1);

			if (oldCapacity != aVector.capacity())
				printContainerState(aVector.size(), aVector.capacity());
		}
		printContainerState(aVector.size(), aVector.capacity());
	}

}

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:33
Processing time 0.0137 sec