자바 현재 시간 측정하기 성능 측정하기

코드늘보·2024년 9월 24일

System.currentTimeMillis() 은 1970년 1월 1일 00:00:00 UTC를 시작으로 현재까지 경과한 시간을 1/1000초 단위로 나타내줌;

1970년이 기준인 이유
(https://www.baeldung.com/linux/epoch-time)

밀리초를 현재 시간으로 표시하기

import java.util.Date;

public class Main {
    public static void main(String[] args) {
        // 현재 시간을 밀리초 단위로 가져오기
        long currentTimeMillis = System.currentTimeMillis();

        // 밀리초 값을 Date 객체로 변환
        Date date = new Date(currentTimeMillis);

        // Date 객체를 출력 (자동으로 읽기 쉬운 형식으로 출력됨)
        System.out.println("현재 시간: " + date);
    }
}

Stringbuffer에 별을 50000개 추가하는 시간 측정하기

시간 측정1 -------┐

~~ [함수] ~~ 함수 실행 시간

시간 측정2 -------┘

public class Main extends Thread{
    public static void main(String[] args) {
        int index = 50000;
        StringBuffer item = new StringBuffer();
        long startTime1 = System.currentTimeMillis();
        for (int i = 0; i < index; i++) {
            item.append("*");
        }
        long endTime1 = System.currentTimeMillis();
        long runningTime1 = endTime1 - startTime1;
        System.out.println("실행시간 : " + runningTime1);
    }
}
profile
쉬운 코드를 지향합니다.

0개의 댓글