เปรียบเทียบแอป
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
การเปรียบเทียบเป็นวิธีตรวจสอบและติดตามประสิทธิภาพของแอป คุณสามารถทำการเปรียบเทียบเป็นประจำเพื่อวิเคราะห์และแก้ไขปัญหาด้านประสิทธิภาพ รวมถึงช่วยให้มั่นใจได้ว่าการเปลี่ยนแปลงล่าสุดจะไม่ทำให้เกิดการถดถอย
Android มีไลบรารีการเปรียบเทียบและแนวทาง 2 แบบสำหรับการวิเคราะห์และ
ทดสอบสถานการณ์ต่างๆ ในแอป ได้แก่ Macrobenchmark และ
Microbenchmark
มาโครเบนช์มาร์ก
ไลบรารี Macrobenchmark จะวัดการโต้ตอบของผู้ใช้ปลายทางที่ใหญ่ขึ้น เช่น
การเริ่มต้น การโต้ตอบกับ UI และภาพเคลื่อนไหว ไลบรารีช่วยให้คุณควบคุมสภาพแวดล้อมด้านประสิทธิภาพที่คุณกำลังทดสอบได้โดยตรง
ซึ่งช่วยให้คุณควบคุมการคอมไพล์ รวมถึงเริ่มและหยุดแอปเพื่อวัดการเริ่มต้นหรือการเลื่อนแอปจริงได้โดยตรง
ไลบรารี Macrobenchmark จะแทรกเหตุการณ์และตรวจสอบผลลัพธ์ภายนอกจาก
แอปทดสอบที่สร้างขึ้นด้วยการทดสอบของคุณ ดังนั้น เมื่อเขียนการทดสอบประสิทธิภาพ
คุณจะไม่เรียกใช้โค้ดของแอปโดยตรง แต่จะไปยังส่วนต่างๆ ในแอปในฐานะ
ผู้ใช้แทน
Microbenchmark
ไลบรารี Microbenchmark ช่วยให้คุณเปรียบเทียบโค้ดของแอปได้โดยตรงในลูป
ออกแบบมาเพื่อวัดงานของ CPU ที่ประเมินประสิทธิภาพในกรณีที่ดีที่สุด เช่น การเข้าถึงดิสก์ที่แคชไว้และ Just in Time (JIT) ที่อุ่นเครื่องแล้ว ซึ่งคุณอาจเห็นได้ในลูปภายในหรือฟังก์ชันที่ใช้งานบ่อยเฉพาะ ไลบรารีจะวัดได้เฉพาะ
โค้ดที่คุณเรียกใช้ได้โดยตรงในสภาพแวดล้อมที่แยกจากกัน
หากแอปต้องประมวลผลโครงสร้างข้อมูลที่ซับซ้อน หรือมีอัลกอริทึมเฉพาะที่ต้องใช้การคำนวณหนักซึ่งเรียกใช้หลายครั้งในระหว่างการเรียกใช้แอป
กรณีเหล่านี้อาจเป็นกรณีที่ดีสำหรับการเปรียบเทียบ นอกจากนี้ คุณยังวัดส่วนต่างๆ ของ
UI ได้ด้วย เช่น คุณสามารถวัดต้นทุนของRecyclerView
การเชื่อมโยงรายการ
ระยะเวลาที่ใช้ในการขยายเลย์เอาต์ หรือความต้องการของเลย์เอาต์และการวัด
ของคลาส View
จากมุมมองด้านประสิทธิภาพ
อย่างไรก็ตาม คุณจะวัดไม่ได้ว่ากรณีที่เปรียบเทียบมีส่วนช่วยในประสบการณ์ของผู้ใช้โดยรวมอย่างไร
ในบางกรณี การเปรียบเทียบไม่ได้บอกว่าคุณกำลังปรับปรุงคอขวด เช่น Jank หรือเวลาเริ่มต้นแอป หรือไม่ ด้วยเหตุนี้
คุณจึงควรระบุคอขวดเหล่านั้นก่อนด้วย Android Profiler
หลังจากพบโค้ดที่ต้องการตรวจสอบและเพิ่มประสิทธิภาพแล้ว ลูปที่เปรียบเทียบ
สามารถทำงานซ้ำๆ ได้อย่างรวดเร็วและง่ายดายเพื่อสร้างผลลัพธ์ที่มีสัญญาณรบกวนน้อยลง
ซึ่งช่วยให้คุณมุ่งเน้นที่การปรับปรุงด้านใดด้านหนึ่งได้
ไลบรารี Microbenchmark จะรายงานข้อมูลเกี่ยวกับแอปเท่านั้น ไม่ใช่เกี่ยวกับ
ระบบโดยรวม ดังนั้น จึงเหมาะที่สุดสำหรับการวิเคราะห์ประสิทธิภาพของสถานการณ์ที่เฉพาะเจาะจงกับแอป ไม่ใช่สถานการณ์ที่อาจเกี่ยวข้องกับปัญหาของระบบโดยรวม
การเปรียบเทียบคลังข้อมูลการเปรียบเทียบ
|
มาโครเบนช์มาร์ก |
Microbenchmark |
เวอร์ชัน API |
23 ขึ้นไป |
14 ขึ้นไป |
การทำงาน
|
วัดจุดแรกเข้าหรือการโต้ตอบระดับสูง เช่น การเปิดใช้งานหรือการเลื่อนรายการ |
วัดฟังก์ชันแต่ละรายการ
|
ขอบเขต
|
การทดสอบแบบแยกกระบวนการของแอปแบบเต็ม
|
การทดสอบงานของ CPU ในกระบวนการ
|
ความเร็ว
|
ความเร็วในการทำซ้ำปานกลาง โดยอาจ
นานกว่า 1 นาที |
ความเร็วในการทำซ้ำที่รวดเร็ว มักใช้เวลาไม่ถึง 10 วินาที |
การติดตาม
|
ผลลัพธ์มาพร้อมกับร่องรอยการทำโปรไฟล์
|
การสุ่มตัวอย่างและการติดตามเมธอดที่ไม่บังคับ |
แนะนำสำหรับคุณ
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-27 UTC
[[["เข้าใจง่าย","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-08-27 UTC"],[],[],null,["Benchmarking is a way to inspect and monitor the performance of your app. You\ncan regularly run benchmarks to analyze and debug performance problems and help\nensure that you don't introduce regressions in recent changes.\n\nAndroid offers two benchmarking libraries and approaches for analyzing and\ntesting different kinds of situations in your app: Macrobenchmark and\nMicrobenchmark.\n\nMacrobenchmark\n\nThe [Macrobenchmark](/studio/profile/macrobenchmark) library measures larger end-user interactions, such as\nstartup, interacting with the UI, and animations. The library provides direct\ncontrol over the performance environment you're testing. It lets you control\ncompiling and lets you start and stop your app to directly measure actual app\nstartup or scrolling.\n\nThe Macrobenchmark library injects events and monitors results externally from a\ntest app that is built with your tests. Therefore, when writing the benchmarks,\nyou don't call your app code directly and instead navigate within your app as a\nuser.\n\nMicrobenchmark\n\nThe [Microbenchmark](/studio/profile/benchmark) library lets you benchmark app code directly in a loop.\nThis is designed for measuring CPU work that assesses best-case performance---such\nas warmed up Just in Time (JIT) and disk accesses cached---that you might see with\nan inner-loop or a specific hot function. The library can only measure the\ncode that you can call directly in isolation.\n\nIf your app needs to process a complex data structure, or have some specific\ncomputation-heavy algorithm that is called multiple times during the app run,\nthese might be good cases for benchmarking. You can also measure parts of your\nUI. For example, you can measure the cost of the `RecyclerView` item binding,\nhow long it takes to inflate a layout, or how demanding the layout-and-measure\npass of your `View` class is from a performance perspective.\n\nHowever, you aren't able to measure how the benchmarked cases contribute to the\noverall user experience. In some scenarios, benchmarking doesn't tell you if\nyou're improving a bottleneck like jank or app startup time. For this reason,\nit's crucial to identify those bottlenecks first with the [Android Profiler](/studio/profile).\nAfter you find the code you want to investigate and optimize, the benchmarked\nloop can run repeatedly in a quick and easier fashion to create less noisy\nresults, which lets you focus on one area of improvement.\n\nThe Microbenchmark library only reports information about your app, not about\nthe system overall. Therefore, it's best at analyzing performance of situations\nspecific to the app, not ones that might relate to overall system issues.\n\nBenchmark library comparison\n\n| | Macrobenchmark | Microbenchmark |\n|-------------|-----------------------------------------------------------------------------------------------|---------------------------------------------------|\n| API version | 23 and later | 14 and later |\n| Function | Measure high-level entry points or interactions, such as activity launch or scrolling a list. | Measure individual functions. |\n| Scope | Out-of-process test of full app. | In-process test of CPU work. |\n| Speed | Medium iteration speed. It can exceed a minute. | Fast iteration speed. Often less than 10 seconds. |\n| Tracing | Results come with profiling traces. | Optional method sampling and tracing. |\n\nRecommended for you\n\n- Note: link text is displayed when JavaScript is off\n- [Create Baseline Profiles {:#creating-profile-rules}](/topic/performance/baselineprofiles/create-baselineprofile)\n- [JankStats Library](/topic/performance/jankstats)\n- [Overview of measuring app performance](/topic/performance/measuring-performance)"]]