# 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)

*** ** * ** ***


```
open class MicrobenchmarkScope
```

<br />

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

|---|---|
| `https://developer.android.com/reference/kotlin/androidx/benchmark/junit4/BenchmarkRule.Scope` | Handle used for controlling measurement during `https://developer.android.com/reference/kotlin/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 functions |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/benchmark/MicrobenchmarkScope#pauseMeasurement()()` Pause measurement until the next call to `https://developer.android.com/reference/kotlin/androidx/benchmark/MicrobenchmarkScope#resumeMeasurement()`. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/benchmark/MicrobenchmarkScope#resumeMeasurement()()` Resume measurement after a call to `https://developer.android.com/reference/kotlin/androidx/benchmark/MicrobenchmarkScope#pauseMeasurement()` |
| `inline T` | `<T : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/benchmark/MicrobenchmarkScope#runWithMeasurementDisabled(kotlin.Function0)(block: () -> T)` Disable measurement for a block of code. |

## Public functions

### pauseMeasurement

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

```
fun pauseMeasurement(): Unit
```

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

`https://developer.android.com/reference/kotlin/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/kotlin/androidx/benchmark/MicrobenchmarkScope#runWithMeasurementDisabled(kotlin.Function0)`.

### resumeMeasurement

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

```
fun resumeMeasurement(): Unit
```

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

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

### runWithMeasurementDisabled

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

```
inline fun <T : Any?> runWithMeasurementDisabled(block: () -> T): T
```

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)
}
```