Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Java

วิธีคำนวณ Elapsed Time / Execution Time ใน Java


Elapsed Time (เวลาที่ผ่านไป) หรือ Execution Time (เวลาการทำงาน) คือเวลาจากจุดเริ่มต้นถึงจุดสิ้นสุดของเหตุการณ์หรือการทำงานของเมธอด Java มีหลายวิธีในการวัด

วิธีต่างๆ ในการวัด Elapsed Time

1. System.currentTimeMillis() - มิลลิวินาที

คืนค่าเวลาปัจจุบันเป็นมิลลิวินาที ใช้ได้สำหรับการวัดเวลาที่ยาวพอสมควร

public class Example {
    public void test() {
        int num = 0;
        for (int i = 0; i <= 50; i++) {
            num = num + i;
            System.out.print(num + ", ");
        }
    }
    
    public static void main(String args[]) {
        long begin = System.currentTimeMillis();
        new Example().test();
        long end = System.currentTimeMillis();
        long time = end - begin;
        System.out.println();
        System.out.println("Elapsed Time: " + time + " milli seconds");
    }
}
Elapsed Time: 4 milli seconds

2. System.nanoTime() - นาโนวินาที (แม่นยำกว่า)

public class Example {
    public static void main(String args[]) {
        long begin = System.nanoTime();
        new Example().test();
        long end = System.nanoTime();
        long time = end - begin;
        System.out.println("Elapsed Time: " + time + " nanoseconds");
    }
}
Elapsed Time: 1530200 nanoseconds

3. java.time.Instant + Duration (Java 8+) - แนะนำ

import java.time.Duration;
import java.time.Instant;

public class Example {
    public static void main(String args[]) {
        Instant start = Instant.now();
        new Example().test();
        Instant end = Instant.now();
        long time = Duration.between(start, end).toMillis();
        System.out.println(time + " Milli seconds");
    }
}
3 Milli seconds

4. Apache Commons Lang StopWatch

ต้องเพิ่ม dependency ใน Maven:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.7</version>
</dependency>
import org.apache.commons.lang3.time.StopWatch;

public class Example {
    public static void main(String args[]) {
        StopWatch obj = new StopWatch();
        obj.start();
        new Example().test();
        obj.stop();
        System.out.println("Elapsed Time: " + obj.getTime() + " milli seconds");
    }
}
Elapsed Time: 1 milli seconds

5. java.util.Date.getTime() - Legacy

import java.util.Date;

public class Demo {
    public void test() { /* ... */ }
    
    public static void main(String[] args) {
        long startTime = new Date().getTime();
        new Demo().test();
        long endTime = new Date().getTime();
        long time = endTime - startTime;
        System.out.println("Execution time: " + time);
    }
}
Execution time: 5

6. Calendar.getInstance().getTime().getTime() - Legacy

import java.util.Calendar;

public class Demo {
    public static void main(String[] args) {
        long startTime = Calendar.getInstance().getTime().getTime();
        new Demo().test();
        long endTime = Calendar.getInstance().getTime().getTime();
        long time = endTime - startTime;
        System.out.println("Execution time: " + time);
    }
}
Execution time: 20

ตารางเปรียบเทียบและคำแนะนำ

วิธีความแม่นยำเวอร์ชันคำแนะนำ
System.nanoTime()สูงสุด (นานอวินาที)Java 1.5+เหมาะสำหรับ benchmarking, profiling
Instant + Durationสูง (มิลลิวินาที)Java 8+แนะนำสำหรับโค้ดใหม่ API สมัยใหม่ อ่านง่าย
StopWatch (Apache Commons)มิลลิวินาทีJava 1.5+สะดวก มีฟีเจอร์ครบ (start, stop, reset, split)
System.currentTimeMillis()มิลลิวินาทีJava 1.0+ใช้ได้เร็ว แต่อาจไม่แม่นยำสำหรับเวลาสั้นมาก
Date/CalendarมิลลิวินาทีJava 1.1+Legacy - ไม่แนะนำใช้ใหม่

ベストプラクティス (Best Practices)

  • ใช้ Instant + Duration สำหรับโปรเจกต์ Java 8+
  • ใช้ nanoTime() สำหรับ micro-benchmarking
  • รันหลายรอบและหาค่าเฉลี่ย เพื่อลดผลกระทบจาก JIT compilation และ GC
  • พิจารณาใช้ JMH (Java Microbenchmark Harness) สำหรับ benchmarking จริงจัง