시작하기무료로 시작하기

연산 시간 측정

문자열 연결은 자주 사용하는 연산이지만, 비효율적으로 구현하면 성능 병목이 될 수 있어요. 이 연습 문제에서는 + 연산자를 사용해 문자열을 연결하는 데 걸리는 시간을 측정해 보겠습니다.

이 연습은 강의의 일부입니다

Java 코드 최적화

강의 보기

연습 안내

  • 연산을 수행하기 전과 후에 각각 시작 시간과 종료 시간을 기록하세요.
  • startTimeendTime을 사용해 이번 연산의 전체 소요 시간을 계산하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

public class Main {
    public static void main(String[] args) {
        // Get the start time
        long startTime = System.____();
        
        String result = "";
        for (int i = 0; i < 10000; i++) {
            // Add the current number to the result string
            result += i;
        }
        
        // Get the end time
        long endTime = ____.nanoTime();
        
        // Calculate the duration
        long durationInNanos = ____;
        double durationInMillis = durationInNanos / 1_000_000.0;
        
        System.out.println("String concatenation took: " + durationInMillis + " ms");
        System.out.println("Final string length: " + result.length());
    }
}
코드 편집 및 실행