Stay organized with collections
Save and categorize content based on your preferences.
class BenchmarkState
Control object for microbenchmarking in Java.
Query a state object with androidx.benchmark.junit4.BenchmarkRule.getState
, and use it to measure a block of Java with BenchmarkStateLegacy.keepRunning
:
@Rule
public BenchmarkRule benchmarkRule = new BenchmarkRule();
@Test
public void sampleMethod() {
BenchmarkState state = benchmarkRule.getState();
int[] src = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
while (state.keepRunning()) {
int[] dest = new int[src.length];
System.arraycopy(src, 0, dest, 0, src.length);
}
}
Note that BenchmarkState does not give access to Perfetto traces.
Summary
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-05-15 UTC.
[null,null,["Last updated 2025-05-15 UTC."],[],[],null,["# BenchmarkState\n==============\n\nArtifact: [androidx.benchmark:benchmark-common](/jetpack/androidx/releases/benchmark) \n[View Source](https://cs.android.com/search?q=file:androidx/benchmark/BenchmarkState.kt+class:androidx.benchmark.BenchmarkState) \nAdded in [1.0.0](/jetpack/androidx/releases/benchmark#1.0.0)\n\n*** ** * ** ***\n\nKotlin \\|[Java](/reference/androidx/benchmark/BenchmarkState \"View this page in Java\")\n\n\n```\nclass BenchmarkState\n```\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\nControl object for microbenchmarking in Java.\n\nQuery a state object with [androidx.benchmark.junit4.BenchmarkRule.getState](/reference/kotlin/androidx/benchmark/junit4/BenchmarkRule#getState()), and use it to measure a block of Java with [BenchmarkStateLegacy.keepRunning](/reference/kotlin/androidx/benchmark/BenchmarkStateLegacy#keepRunning()): \n\n```kotlin\n@Rule\npublic BenchmarkRule benchmarkRule = new BenchmarkRule();\n\n@Test\npublic void sampleMethod() {\n BenchmarkState state = benchmarkRule.getState();\n\n int[] src = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };\n while (state.keepRunning()) {\n int[] dest = new int[src.length];\n System.arraycopy(src, 0, dest, 0, src.length);\n }\n}\n```\n\nNote that BenchmarkState does not give access to Perfetto traces.\n\nSummary\n-------\n\n| ### Public functions |\n|-------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------|\n| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | [keepRunning](/reference/kotlin/androidx/benchmark/BenchmarkState#keepRunning())`()` |\n| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [pauseTiming](/reference/kotlin/androidx/benchmark/BenchmarkState#pauseTiming())`()` |\n| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | [resumeTiming](/reference/kotlin/androidx/benchmark/BenchmarkState#resumeTiming())`()` |\n\nPublic functions\n----------------\n\n### keepRunning\n\nAdded in [1.0.0](/jetpack/androidx/releases/benchmark#1.0.0) \n\n```\nfun keepRunning(): Boolean\n``` \n\n### pauseTiming\n\nAdded in [1.0.0](/jetpack/androidx/releases/benchmark#1.0.0) \n\n```\nfun pauseTiming(): Unit\n``` \n\n### resumeTiming\n\nAdded in [1.0.0](/jetpack/androidx/releases/benchmark#1.0.0) \n\n```\nfun resumeTiming(): Unit\n```"]]