Skip to content

Commit

Permalink
Improvements to writing-benchmarks.md doc (#148)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: Abduqodiri Qurbonzoda <[email protected]>
  • Loading branch information
wldeh and Abduqodiri Qurbonzoda authored Sep 1, 2023
1 parent dc32541 commit 5d82b25
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 43 deletions.
47 changes: 24 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,8 @@ Note: Kotlin/WASM is an experimental compilation target for Kotlin. It may be dr

### Writing Benchmarks

After setting up your project and configuring targets, you can start writing benchmarks:
After setting up your project and configuring targets, you can start writing benchmarks.
As an example, let's write a simplified benchmark that tests how fast we can add up numbers in an ArrayList:
1. **Create Benchmark Class**: Create a class in your source set where you'd like to add the benchmark. Annotate this class with `@State(Scope.Benchmark)`.

Expand All @@ -326,12 +327,11 @@ After setting up your project and configuring targets, you can start writing ben
}
```

2. **Set up Parameters and Variables**: Define variables needed for the benchmark.
2. **Set up Variables**: Define variables needed for the benchmark.

```kotlin
var param: Int = 10

private var list: MutableList<Int> = ArrayList()
private val size = 10
private val list = ArrayList<Int>()
```

3. **Initialize Resources**: Within the class, you can define any setup or teardown methods using `@Setup` and `@TearDown` annotations respectively. These methods will be executed before and after the entire benchmark run.
Expand Down Expand Up @@ -361,30 +361,31 @@ After setting up your project and configuring targets, you can start writing ben

Your final benchmark class will look something like this:

@State(Scope.Benchmark)
class MyBenchmark {

var param: Int = 10
```kotlin
@State(Scope.Benchmark)
class MyBenchmark {

private var list: MutableList<Int> = ArrayList()
private val size = 10
private val list = ArrayList<Int>()

@Setup
fun prepare() {
for (i in 0 until size) {
list.add(i)
}
@Setup
fun prepare() {
for (i in 0 until size) {
list.add(i)
}
}

@Benchmark
fun benchmarkMethod(): Int {
return list.sum()
}
@Benchmark
fun benchmarkMethod(): Int {
return list.sum()
}

@TearDown
fun cleanup() {
list.clear()
}
@TearDown
fun cleanup() {
list.clear()
}
}
```

Note: Benchmark classes located in the common source set will be run in all platforms, while those located in a platform-specific source set will be run only in the corresponding platform.

Expand Down
Loading

0 comments on commit 5d82b25

Please sign in to comment.