본문 바로가기
프로그래밍/JAVA

[JAVA] 프로그램 실행 시간 측정 및 출력

by 소나기_레드 2023. 2. 28.
반응형

long lStart = System.currentTimeMillis(); // 시작 시간 밀리세컨

//long lStart = System.nanoTime(); // 시작 시간 나노세컨

//////////

실행 시간을 측정하고자 하는 코드

//////////

 

long lEnd = System.currentTimeMillis(); // 종료 시간 밀리세컨

//long lEnd = System.nanoTime(); // 종료 시간 나노세컨

 

float fGapTime = lEnd - lStart / 1000f ; // 실행 시간을 초로 변환

//float fGapTime = lEnd - lStart / 1000000000f ; // 실행 시간을 초로 변환

System.out.println("%.2f", fGapTime); //소수점 2자리까지 출력

시간 단위

1초

= 0.001초(밀리초ms) = 10^(-3) = 1/1,000

= 0.000001초(마이크로초㎲) = 10^(-6) = 1/1,000,000

= 0.000000001초(나노초ns) = 10^(-9) = 1/1,000,000,000

= 0.000000000001초(피코초ps) = 10^(-12) = 1/1,000,000,000,000

= 0.000000000000001초(펨토초fs) = 10^(-15) = 1/1,000,000,000,000,000

= 0.000000000000000001초(아토초as)= 10^(-18) = 1/1,000,000,000,000,000,000

<출력 관련 참고 매뉴얼>

https://docs.oracle.com/javase/tutorial/java/data/numberformat.html

<참고>

The DecimalFormat Class

You can use the java.text.DecimalFormat class to control the display of leading and trailing zeros, prefixes and suffixes, grouping (thousands) separators, and the decimal separator. DecimalFormat offers a great deal of flexibility in the formatting of numbers, but it can make your code more complex.

The example that follows creates a DecimalFormat object, myFormatter, by passing a pattern string to the DecimalFormat constructor. The format() method, which DecimalFormat inherits from NumberFormat, is then invoked by myFormatter—it accepts a double value as an argument and returns the formatted number in a string:

Here is a sample program that illustrates the use of DecimalFormat:

import java.text.*;

public class DecimalFormatDemo {

static public void customFormat(String pattern, double value ) {

DecimalFormat myFormatter = new DecimalFormat(pattern);

String output = myFormatter.format(value);

System.out.println(value + " " + pattern + " " + output);

}

static public void main(String[] args) {

customFormat("###,###.###", 123456.789);

customFormat("###.##", 123456.789);

customFormat("000000.000", 123.78);

customFormat("$###,###.###", 12345.67);

}

}

The output is:

123456.789 ###,###.### 123,456.789

123456.789 ###.## 123456.79

123.78 000000.000 000123.780

12345.67 $###,###.### $12,345.67

반응형

댓글