2

I am looking for utility or any way how can I measure the time required for inflater to inflate some portion (view or viewgroup or whole screen) in Android.

I know there is default feature in debug mode that shows overdraw, but I need to have measure the time taken to inflate view.

What is the best way to do this. Thx.

3 Answers3

4

Check out lucasr/probe. Here is a blog post by a Facebook engineer on improving view measure time.

Hierarchy Viewer is also a useful visual tool to see which part of your layout can be improved.

hidro
  • 12,333
  • 6
  • 53
  • 53
3

In 2019, you can use android x benchmark library easy for benchmark inflate time. android doc write-benchmark

@RunWith(AndroidJUnit4::class)
class ViewBenchmark {
    @get:Rule
    val benchmarkRule = BenchmarkRule()

    @Test
    fun simpleViewInflate() {
        val context = ApplicationProvider.getApplicationContext()
        val inflater = LayoutInflater.from(context)
        val root = FrameLayout(context)

        benchmarkRule.keepRunning {
            inflater.inflate(R.layout.test_simple_view, root, false)
        }
    }
}

or you can use system trace to benchmark only inflate layout code

qianlv
  • 502
  • 5
  • 15
0

In 2022, you have androidx.benchmark library where you can measure layout inflation time. Check out the sample code here from Google. https://github.com/android/performance-samples

ViewInflateBenchmark.kt

@RunWith(AndroidJUnit4::class)
class ViewInflateBenchmark {

    @get:Rule
    val benchmarkRule = BenchmarkRule()

    @Test
    fun benchmarkViewInflate() {
        val context = InstrumentationRegistry.getInstrumentation().context
        val inflater = LayoutInflater.from(context)
        val root = FrameLayout(context)

        benchmarkRule.measureRepeated {
            val inflated = inflater.inflate(R.layout.item_card, root, false)
        }
    }
}
AndroidDev
  • 1,485
  • 2
  • 18
  • 33