# MicrobenchmarkScope

Artifact: [androidx.benchmark:benchmark-common](https://developer.android.com/jetpack/androidx/releases/benchmark) [View Source](https://cs.android.com/search?q=file:androidx/benchmark/Microbenchmark.kt+class:androidx.benchmark.MicrobenchmarkScope) Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/benchmark#1.4.0)

*** ** * ** ***


```
public class MicrobenchmarkScope
```

<br />

Known direct subclasses [BenchmarkRule.Scope](https://developer.android.com/reference/androidx/benchmark/junit4/BenchmarkRule.Scope)

|---|---|
| `https://developer.android.com/reference/androidx/benchmark/junit4/BenchmarkRule.Scope` | Handle used for controlling measurement during `https://developer.android.com/reference/androidx/benchmark/junit4/package-summary#(androidx.benchmark.junit4.BenchmarkRule).measureRepeated(kotlin.Function1)`. |

*** ** * ** ***

Scope handle for pausing/resuming microbenchmark measurement.

This is functionally an equivalent to `BenchmarkRule.Scope` which will work without the JUnit dependency.

## Summary

| ### Public methods |
|---|---|
| `final void` | `https://developer.android.com/reference/androidx/benchmark/MicrobenchmarkScope#pauseMeasurement()()` Pause measurement until the next call to `https://developer.android.com/reference/androidx/benchmark/MicrobenchmarkScope#resumeMeasurement()`. |
| `final void` | `https://developer.android.com/reference/androidx/benchmark/MicrobenchmarkScope#resumeMeasurement()()` Resume measurement after a call to `https://developer.android.com/reference/androidx/benchmark/MicrobenchmarkScope#pauseMeasurement()` |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull T` | `<T extends https://developer.android.com/reference/java/lang/Object.html> https://developer.android.com/reference/androidx/benchmark/MicrobenchmarkScope#runWithMeasurementDisabled(kotlin.Function0)(@https://developer.android.com/reference/androidx/annotation/NonNull Function0<@https://developer.android.com/reference/androidx/annotation/NonNull T> block)` Disable measurement for a block of code. |

## Public methods

### pauseMeasurement

Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/benchmark#1.4.0)

```
public final void pauseMeasurement()
```

Pause measurement until the next call to `https://developer.android.com/reference/androidx/benchmark/MicrobenchmarkScope#resumeMeasurement()`.

`https://developer.android.com/reference/androidx/benchmark/MicrobenchmarkScope#resumeMeasurement()` must be called before exiting the measurement loop.

Re-entrant pausing is not supported.

Kotlin callers should generally instead use `https://developer.android.com/reference/androidx/benchmark/MicrobenchmarkScope#runWithMeasurementDisabled(kotlin.Function0)`.

### resumeMeasurement

Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/benchmark#1.4.0)

```
public final void resumeMeasurement()
```

Resume measurement after a call to `https://developer.android.com/reference/androidx/benchmark/MicrobenchmarkScope#pauseMeasurement()`

Kotlin callers should generally instead use `https://developer.android.com/reference/androidx/benchmark/MicrobenchmarkScope#runWithMeasurementDisabled(kotlin.Function0)`.

### runWithMeasurementDisabled

Added in [1.4.0](https://developer.android.com/jetpack/androidx/releases/benchmark#1.4.0)

```
public final @NonNull T <T extends Object> runWithMeasurementDisabled(@NonNull Function0<@NonNull T> block)
```

Disable measurement for a block of code.

Used for disabling timing/measurement for work that isn't part of the benchmark:

- When constructing per-loop randomized inputs for operations with caching,

- Controlling which parts of multi-stage work are measured (e.g. View measure/layout)

- Per-loop verification

```kotlin
import androidx.benchmark.junit4.measureRepeated

@Test
fun bitmapProcessing() = benchmarkRule.measureRepeated {
    val input: Bitmap = runWithMeasurementDisabled { constructTestBitmap() }
    processBitmap(input)
}
```