最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java8如何優(yōu)雅的記錄代碼運行時間

 更新時間:2025年04月20日 09:47:29   作者:李博帥  
這篇文章主要為大家詳細介紹了 Java 8 中幾種記錄代碼運行時間的優(yōu)雅方式,并附上實用工具類與建議,希望可以幫助大家提高大家的代碼可讀性與復用性

在日常后端開發(fā)中,性能優(yōu)化是一項核心任務。我們經(jīng)常需要測量某段代碼的執(zhí)行耗時,例如查詢耗時、接口響應時間、批處理任務處理時間等。在 Java 中,傳統(tǒng)的做法可能是使用 System.currentTimeMillis():

long start = System.currentTimeMillis();
// 業(yè)務邏輯
long end = System.currentTimeMillis();
System.out.println("執(zhí)行耗時: " + (end - start) + "ms");

雖然這非常直接,但在 Java 8 引入 java.time 包之后,我們可以使用更現(xiàn)代、更語義化的方式 —— Instant 和 Duration 來實現(xiàn)這一目標。

本文將帶你深入了解 Java 8 中幾種記錄代碼運行時間的優(yōu)雅方式,并附上實用工具類與建議,提高你的代碼可讀性與復用性。

Java 8 簡單實現(xiàn)方式

Java 8 中的 java.time.Instant 表示一個時間點,Duration 表示兩個時間點之間的時長:

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

public class SampleTimer {
    public static void main(String[] args) {
        Instant start = Instant.now();

        // 模擬業(yè)務邏輯
        executeBusinessLogic();

        Instant end = Instant.now();
        long timeElapsed = Duration.between(start, end).toMillis();
        System.out.println("執(zhí)行耗時: " + timeElapsed + " ms");
    }

    private static void executeBusinessLogic() {
        try {
            Thread.sleep(500); // 模擬耗時操作
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
    }
}

相比 System.currentTimeMillis(),這種寫法更具可讀性和表達力,并且跨平臺表現(xiàn)一致(System.currentTimeMillis 可能受系統(tǒng)時間調(diào)整影響)。

封裝計時工具類(更通用 & 可復用)

為了更好地管理運行時間測量代碼,我們可以封裝一個小型工具類,例如 TimerUtils:

import java.time.Duration;
import java.time.Instant;
import java.util.function.Supplier;

public class TimerUtils {

    public static <T> T measure(String taskName, Supplier<T> supplier) {
        Instant start = Instant.now();
        try {
            return supplier.get();
        } finally {
            Instant end = Instant.now();
            long duration = Duration.between(start, end).toMillis();
            System.out.printf("【任務: %s】耗時: %d ms%n", taskName, duration);
        }
    }

    public static void measure(String taskName, Runnable runnable) {
        Instant start = Instant.now();
        try {
            runnable.run();
        } finally {
            Instant end = Instant.now();
            long duration = Duration.between(start, end).toMillis();
            System.out.printf("【任務: %s】耗時: %d ms%n", taskName, duration);
        }
    }
}

使用方式如下:

public class TimerUtilsTest {
    public static void main(String[] args) {
        // 沒有返回值
        TimerUtils.measure("執(zhí)行任務A", () -> {
            try {
                Thread.sleep(200);
            } catch (InterruptedException ignored) {}
        });

        // 有返回值
        String result = TimerUtils.measure("執(zhí)行任務B", () -> {
            try {
                Thread.sleep(300);
            } catch (InterruptedException ignored) {}
            return "任務完成";
        });

        System.out.println("任務B返回結果:" + result);
    }
}

這種封裝方式可以很好地提升代碼的可讀性、復用性,尤其適合在 Spring Boot 項目中進行日志輸出分析。

進階使用:結合日志框架輸出

實際項目中,推薦的做法是將耗時統(tǒng)計信息輸出到日志中,便于統(tǒng)一采集和排查性能瓶頸。結合 SLF4J + Logback,可以這樣集成:

private static final Logger logger = LoggerFactory.getLogger(MyService.class);

public void process() {
    Instant start = Instant.now();

    try {
        // 執(zhí)行業(yè)務邏輯
    } finally {
        long elapsed = Duration.between(start, Instant.now()).toMillis();
        logger.info("處理接口耗時: {} ms", elapsed);
    }
}

比 System.out.println 更專業(yè),也更便于后續(xù) ELK/Prometheus 等監(jiān)控系統(tǒng)采集。

使用 AOP 自動記錄方法執(zhí)行時間(Spring Boot 推薦)

性能日志可以通過 Spring AOP 自動記錄,無需在每個方法中手動添加計時代碼:

1.自定義注解:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Timed {
    String value() default "";
}

2.編寫切面類:

@Aspect
@Component
public class TimingAspect {
    private static final Logger logger = LoggerFactory.getLogger(TimingAspect.class);

    @Around("@annotation(timed)")
    public Object around(ProceedingJoinPoint pjp, Timed timed) throws Throwable {
        Instant start = Instant.now();
        Object result = pjp.proceed();
        long elapsed = Duration.between(start, Instant.now()).toMillis();

        String methodName = pjp.getSignature().toShortString();
        logger.info("方法 [{}] 執(zhí)行耗時: {} ms", methodName, elapsed);
        return result;
    }
}

使用示例:

@Timed
public void handleRequest() {
    // 邏輯代碼
}

優(yōu)勢:

  • 解耦業(yè)務邏輯與日志統(tǒng)計
    • 透明統(tǒng)一統(tǒng)計性能數(shù)據(jù)
    • 對 Controller、Service 層尤為實用

其他替代品推薦

除了 Java 原生類,還有一些開源工具類也支持高效計時:

Guava 的 Stopwatch:

Stopwatch stopwatch = Stopwatch.createStarted();
// 執(zhí)行代碼
stopwatch.stop();
System.out.println("耗時: " + stopwatch.elapsed(TimeUnit.MILLISECONDS) + "ms");

Micrometer 和 Spring Boot Actuator 提供的 @Timed 注解和 Metrics 接入更強大采集工具,如 Prometheus、Grafana。

總結

在注重性能的后端開發(fā)中,精準、高效地記錄代碼執(zhí)行時間是不可或缺的一步。本文從 Java 8 原生的 Instant/Duration 入手,展示了編寫更優(yōu)雅、規(guī)范、可擴展的代碼計時方式。

方法優(yōu)雅度可復用性推薦度
System.currentTimeMillis一般?
Instant + Duration良好?
工具類封裝??
AOP 自動化記錄極高極高???

到此這篇關于Java8如何優(yōu)雅的記錄代碼運行時間的文章就介紹到這了,更多相關Java8記錄代碼運行時間內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

鹤峰县| 兴海县| 南昌县| 宁国市| 河曲县| 淄博市| 万源市| 林甸县| 玉环县| 仁化县| 益阳市| 渑池县| 调兵山市| 天峨县| 曲松县| 新宁县| 祁门县| 宜兴市| 平山县| 和龙市| 和林格尔县| 新乐市| 板桥市| 浪卡子县| 呼伦贝尔市| 邳州市| 双桥区| 莱阳市| 杭锦旗| 团风县| 南华县| 凤台县| 旬邑县| 婺源县| 中西区| 台中市| 太保市| 郑州市| 酒泉市| 正蓝旗| 玛多县|