실행환경 | |
Notebook | SAMSUNG NT550p5c-s61r |
CPU | Intel Core i5-3210M 2.50GHz |
Memory | 8 GB |
OS | Window 7 ultimate 64bit |
Java | 1.7.0_51 |
| Android | SDK : 4.4.2 (KitKat) / 테스트기기 : Galaxy S3 4.3 (Jelly Bean) |
WebServer | Apache Tomcat 7.0 |
코드 실행시간 확인하기
조금 더 정확한 측정 방법
public class TimerTest {
public static void main(String[] args) {
ExecTime timer = new ExecTime();
timer.start();
// code
for (int i = 0; i < 100000000; i++) {
}
timer.stop();
timer.printExecTime();
System.out.println(timer.getRunTimeNano() / 1000000 + " ms");
System.out.println(timer.getRunTimeNano() / 1000000 / 1000 + " sec");
}
}
class ExecTime {
private long start;
private long stop;
void start() {
start = System.nanoTime();
}
void stop() {
stop = System.nanoTime();
}
long getRunTimeNano() {
return stop - start;
}
void printExecTime() {
System.out.println(stop - start + " ns");
}
}
방법1
class TimeTest1 {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
// code
long stopTime = System.currentTimeMillis();
long runTime = stopTime - startTime;
System.out.println(runTime);
}
}
방법 2
public class Stopwatch {
long start;
long end;
void start() {
start = System.currentTimeMillis();
}
void stop() {
end = System.currentTimeMillis();
}
String getRunTime(){
return end - start + "";
}
}
class TimeTest2 {
public static void main(String[] args) {
Stopwatch timer = new Stopwatch().start();
// code
timer.stop();
System.out.println(timer.getRunTime());
}
}
'대학 생활 > JAVA' 카테고리의 다른 글
| [JAVA] java.io.RandomAccessFile() (작성중) (0) | 2014.05.27 |
|---|---|
| [JAVA] 전기 요금 계산 (0) | 2014.05.14 |
| [JAVA] 연산자 우선순위 (0) | 2014.05.02 |
| [JAVA] 파일 읽기, 쓰기 (0) | 2014.04.16 |