在編譯腳本/工具類中記錄編譯前後時間戳:
public class CompileTimer {
public static void main(String[] args) {
long start = System.currentTimeMillis();
// 執行編譯邏輯:可調用 JavaCompiler API 或 Runtime.exec("javac ...")
// 此處示例:模擬編譯耗時
try { Thread.sleep(1234); } catch (InterruptedException ignored) {}
long end = System.currentTimeMillis();
System.out.println("編譯耗時: " + (end - start) + " ms");
}
}
maven-compiler-plugin 無內建計時,可用 maven-profile-plugin 或 CI 系統 (Jenkins/GitLab CI) 統計階段耗時。--profile 參數生成 HTML 報告含任務耗時。javax.tools.JavaCompiler API 程式化編譯並計時。JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fm = compiler.getStandardFileManager(null, null, null);
Iterable extends JavaFileObject> files = fm.getJavaFileObjectsFromFiles(Arrays.asList(new File("Main.java")));
long start = System.nanoTime();
JavaCompiler.CompilationTask task = compiler.getTask(null, fm, null, null, null, files);
boolean success = task.call();
long end = System.nanoTime();
System.out.printf("編譯 %s, 耗時 %.2f ms%n", success ? "成功" : "失敗", (end - start) / 1e6);
System.currentTimeMillis() 精度 ~10-15ms,**微基準測試請用 System.nanoTime()** (納秒級單調時鐘)。