使用基準設定檔
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
透過避免包含程式碼路徑的解釋和及時 (JIT) 編譯步驟,基準設定檔可將首次啟動時的程式碼執行速度加快約 30%。如果在應用程式或程式庫中傳送基準設定檔,就能讓 Android 執行階段 (ART) 透過預先 (AOT) 編譯,在新的應用程式安裝和每次應用程式更新時達到效能提升。憑藉此設定檔導引最佳化 (PGO),應用程式可最佳化啟動作業、減少操作卡頓情形,並改善使用者首次啟動時的執行階段整體效能。
Compose 效能注意事項
Compose 是以程式庫的形式發布,而不是 Android 平台的一部分。這種方法可讓 Compose 團隊經常更新 Compose,並支援各種 Android 版本。不過,將 Compose 發布為程式庫會產生費用。
這是因為,Compose 不像 Android 平台程式碼那般,早已經過編譯並安裝在裝置上;
不過,程式庫必須在應用程式啟動時載入,並視需求解譯 JIT。如此一來,應用程式在啟動之際和在首次使用程式庫功能時,執行速度都可能變得較慢。
基準設定檔的優點
您可以定義基準設定檔來改善效能。這些設定檔會定義關鍵使用者旅程所需的類別和方法,並與應用程式的 APK 或 AAB 發布。在應用程式安裝期間,ART 會編譯這個重要程式碼 AOT,以便在應用程式啟動時準備就緒。
良好的基準設定檔定義不一定直接了當,因此 Compose 預設隨附一個定義。您可能無須進行任何操作,就能看到這項福利。不過,與 Compose 隨附的基準設定檔僅包含 Compose 程式庫中程式碼的最佳化。
Macrobenchmark
為取得最佳最佳化效果,請為採用 Macrobenchmark 的應用程式建立基準設定檔,以涵蓋關鍵使用者旅程。定義自己的設定檔時,您必須測試設定檔,驗證該設定檔是否實用。有個理想的做法是撰寫應用程式的 Macrobenchmark 測試,並在寫入及修改基準設定檔時查看測試結果。
請參閱 Macrobenchmark Compose 範例,瞭解如何編寫 Compose UI 的 Macrobenchmark 測試。
其他資源
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Use a baseline profile\n\n[Baseline Profiles](/baseline-profiles) improve code execution speed by about 30% from the first\nlaunch by avoiding interpretation and just-in-time (JIT) compilation steps for\nincluded code paths. By shipping a Baseline Profile in an app or library, you\nenable Android Runtime (ART) to optimize included code paths through\nahead-of-time (AOT) compilation, providing performance enhancements for every\nnew app install and every app update. This profile-guided optimization (PGO)\nlets apps optimize startup, reduce interaction jank, and improve overall runtime\nperformance from the first launch for end users.\n\nCompose performance considerations\n----------------------------------\n\nCompose is distributed as a library instead of as part of the Android platform.\nThis approach lets the Compose team update Compose frequently and support a wide\nrange of Android versions. However, distributing Compose as a library imposes a\ncost.\n\nAndroid platform code is already compiled and installed on the device.\nLibraries, however, need to be loaded when the app launches and interpreted JIT\nwhen needed. This can slow the app on startup and when it uses a library feature\nfor the first time.\n\nBenefits of baseline profiles\n-----------------------------\n\nYou can improve performance by defining [Baseline Profiles](/topic/performance/baselineprofiles/overview). These profiles\ndefine classes and methods needed on critical user journeys and are distributed\nwith your app's APK or AAB. During app installation, ART compiles this critical\ncode AOT so that it's ready for use when the app launches.\n\nA good Baseline Profile definition is not always straightforward, and because\nof this, Compose ships with one by default. You might not have to do any work to\nsee this benefit. However, the Baseline Profile that ships with Compose only\ncontains optimizations for the code within the Compose library.\n\n### Macrobenchmark\n\nTo get the best optimization, [create a Baseline Profile](/topic/performance/baselineprofiles/create-baselineprofile)\nfor your app that uses [Macrobenchmark](/studio/profile/macrobenchmark-overview) to cover critical user journeys. When\nyou define your own profile, you must test the profile to verify that it's\nhelping. A good way to do that is to write [Macrobenchmark](/studio/profile/macrobenchmark-overview) tests for your\napp and check the test results as you write and revise your Baseline Profile.\n\nFor an example of how to write Macrobenchmark tests for your Compose UI, see the\n[Macrobenchmark Compose sample](https://github.com/android/performance-samples/tree/main/MacrobenchmarkSample).\n\nAdditional Resources\n--------------------\n\n- **[App performance guide](/topic/performance/overview)**: Discover best practices, libraries, and tools to improve performance on Android.\n- **[Inspect Performance](/topic/performance/inspecting-overview):** Inspect app performance.\n- **[Benchmarking](/topic/performance/benchmarking/benchmarking-overview):** Benchmark app performance.\n- **[App startup](/topic/performance/appstartup/analysis-optimization):** Optimize app startup.\n- **[Baseline profiles](/baseline-profiles):** Understand baseline profiles."]]