Skip to content

Commit

Permalink
test: 리스트에서 일정 개수 랜덤 선택 테스트 #60
Browse files Browse the repository at this point in the history
  • Loading branch information
khw7385 committed Feb 8, 2025
1 parent 1ffc824 commit 38ebeb9
Showing 1 changed file with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.Random;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;

public class RandomPerformanceTest {

Expand All @@ -27,8 +28,8 @@ void testMathRandom(){
void testRandom(){
long startTime = System.currentTimeMillis();

Random random = new Random();
for(int i = 0; i < 100000000; i++){
Random random = new Random();
random.nextInt(6);
}

Expand All @@ -50,4 +51,43 @@ void testThreadLocalRandom(){

System.out.println("시간(ms): " + (endTime - startTime));
}

@Test
@DisplayName("리스트에서 랜덤 여러 개 선택 - Collection shuffle 이용")
void testSelectUsingCollectionShuffle(){
List<Integer> intList = new ArrayList<>(IntStream.range(0, 10) //IntStream.range(0, 1000000)
.boxed()
.toList());

long startTime = System.currentTimeMillis();

Collections.shuffle(intList);

System.out.println(intList.subList(0, 4));
long endTime = System.currentTimeMillis();

System.out.println("시간(ms): " + (endTime - startTime));
}

@Test
@DisplayName("리스트에서 랜덤 여러 개 선택 - ThreadLocalRandom 사용")
void testSelectUsingThreadLocalRandom(){
List<Integer> intList = new ArrayList<>(IntStream.range(0, 10)//IntStream.range(0, 1000000)
.boxed()
.toList());

Set<Integer> result = new HashSet<>();

long startTime = System.currentTimeMillis();

ThreadLocalRandom random = ThreadLocalRandom.current();
while(result.size() < 4){
result.add(random.nextInt(intList.size()));
}

System.out.println(result);
long endTime = System.currentTimeMillis();

System.out.println("시간(ms): " + (endTime - startTime));
}
}

0 comments on commit 38ebeb9

Please sign in to comment.