Skip to content

Commit

Permalink
feat(study): 레퍼런스 타입과 원시 타입 박싱 학습 테스트 보완 (#8)
Browse files Browse the repository at this point in the history
* feat(study): 레퍼런스 타입과 원시 타입 박싱 학습 테스트 보완

* style: 개행 제거
  • Loading branch information
0chil authored Nov 3, 2024
1 parent a03790e commit f82fdb5
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions src/test/java/com/demo/feedsystemdesign/study/BoxingTest.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,60 @@
package com.demo.feedsystemdesign.study;

import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;

import java.time.Duration;
import java.time.Instant;

import static org.assertj.core.api.Assertions.assertThat;

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
public class BoxingTest {

@Test
void 십만개를_박싱_및_언박싱한다() {
Duration unboxingAndBoxing = time(() -> {
void 박싱이_없는_경우가_가장_빠르며_박싱_및_언박싱이_거듭될수록_느려진다() {
Duration neverBox = time(() -> {
long a = 0L;
for (long i = 0; i < 100_000; i++) {
a = a + i; // 박싱과 언박싱 없음
}
});
Duration unboxAndBox = time(() -> {
Long a = 0L;
for (long i = 0; i < 100_000; i++) {
a = a + i; // 더하는 연산에서 언박싱 발생 -> a 대입 과정에서 박싱 발생
}
});
Duration noBoxing = time(() -> {
Duration unboxBothThenBox = time(() -> {
Long a = 0L;
for (Long i = 0L; i < 100_000; i++) {
a = a + i; // 더하는 연산에서 두 변수 모두 언박싱 발생 -> a 대입 과정에서 박싱 발생
}
});

assertThat(neverBox).isLessThan(unboxAndBox);
assertThat(unboxAndBox).isLessThan(unboxBothThenBox);
}

@Test
void 가장_느린_경우와_가장_빠른_경우에도_유의미한_차이는_없다() {
Duration operationsWithoutBoxing = time(() -> {
long a = 0L;
for (long i = 0; i < 100_000; i++) {
a = a + i; // 박싱과 언박싱 없음
a = a + i;
}
});
Duration unboxingBothAndBoxing = time(() -> {
Duration operationsWithUnboxingAndBoxing = time(() -> {
Long a = 0L;
for (Long i = 0L; i < 100_000; i++) {
a = a + i; // 더하는 연산에서 두 변수 모두 언박싱 발생 -> a 대입 과정에서 박싱 발생
a = a + i;
}
});

assertThat(noBoxing).isLessThan(unboxingAndBoxing);
assertThat(unboxingAndBoxing).isLessThan(unboxingBothAndBoxing);

assertThat(operationsWithoutBoxing).isCloseTo(operationsWithUnboxingAndBoxing, Duration.ofMillis(10));
}


private Duration time(Runnable runnable) {
Instant start = Instant.now();
runnable.run();
Expand Down

0 comments on commit f82fdb5

Please sign in to comment.