Skip to content

Commit 5d82b25

Browse files
wldehAbduqodiri Qurbonzoda
andauthored
Improvements to writing-benchmarks.md doc (#148)
--------- Co-authored-by: Abduqodiri Qurbonzoda <[email protected]>
1 parent dc32541 commit 5d82b25

File tree

2 files changed

+103
-43
lines changed

2 files changed

+103
-43
lines changed

README.md

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,8 @@ Note: Kotlin/WASM is an experimental compilation target for Kotlin. It may be dr
315315

316316
### Writing Benchmarks
317317

318-
After setting up your project and configuring targets, you can start writing benchmarks:
318+
After setting up your project and configuring targets, you can start writing benchmarks.
319+
As an example, let's write a simplified benchmark that tests how fast we can add up numbers in an ArrayList:
319320
320321
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)`.
321322

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

329-
2. **Set up Parameters and Variables**: Define variables needed for the benchmark.
330+
2. **Set up Variables**: Define variables needed for the benchmark.
330331

331332
```kotlin
332-
var param: Int = 10
333-
334-
private var list: MutableList<Int> = ArrayList()
333+
private val size = 10
334+
private val list = ArrayList<Int>()
335335
```
336336

337337
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.
@@ -361,30 +361,31 @@ After setting up your project and configuring targets, you can start writing ben
361361

362362
Your final benchmark class will look something like this:
363363

364-
@State(Scope.Benchmark)
365-
class MyBenchmark {
366-
367-
var param: Int = 10
364+
```kotlin
365+
@State(Scope.Benchmark)
366+
class MyBenchmark {
368367

369-
private var list: MutableList<Int> = ArrayList()
368+
private val size = 10
369+
private val list = ArrayList<Int>()
370370

371-
@Setup
372-
fun prepare() {
373-
for (i in 0 until size) {
374-
list.add(i)
375-
}
371+
@Setup
372+
fun prepare() {
373+
for (i in 0 until size) {
374+
list.add(i)
376375
}
376+
}
377377

378-
@Benchmark
379-
fun benchmarkMethod(): Int {
380-
return list.sum()
381-
}
378+
@Benchmark
379+
fun benchmarkMethod(): Int {
380+
return list.sum()
381+
}
382382

383-
@TearDown
384-
fun cleanup() {
385-
list.clear()
386-
}
383+
@TearDown
384+
fun cleanup() {
385+
list.clear()
387386
}
387+
}
388+
```
388389

389390
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.
390391

0 commit comments

Comments
 (0)