Skip to content

Commit 4049cbb

Browse files
author
Abduqodiri Qurbonzoda
committed
Fix the writing benchmarks code snippet
1 parent c9a4249 commit 4049cbb

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

README.md

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -329,16 +329,15 @@ After setting up your project and configuring targets, you can start writing ben
329329

330330
```kotlin
331331
private val size = 10
332-
333-
private val list = ArrayList()
332+
private val list = ArrayList<Int>()
334333
```
335334

336335
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.
337336

338337
```kotlin
339338
@Setup
340339
fun prepare() {
341-
for (i in 0 until size) {
340+
for (i in 0..<size) {
342341
list.add(i)
343342
}
344343
}
@@ -349,7 +348,7 @@ After setting up your project and configuring targets, you can start writing ben
349348
}
350349
```
351350

352-
4. **Define Benchmark Method**: Next, create methods that you would like to be benchmarked within this class and annotate them with `@Benchmark`.
351+
4. **Define Benchmark Methods**: Next, create methods that you would like to be benchmarked within this class and annotate them with `@Benchmark`.
353352

354353
```kotlin
355354
@Benchmark
@@ -360,30 +359,32 @@ After setting up your project and configuring targets, you can start writing ben
360359

361360
Your final benchmark class will look something like this:
362361

363-
@State(Scope.Benchmark)
364-
class MyBenchmark {
365-
366-
private val size = 10
362+
```kotlin
363+
import kotlinx.benchmark.*
367364

368-
private val list = ArrayList()
365+
@State(Scope.Benchmark)
366+
class MyBenchmark {
367+
private val size = 10
368+
private val list = ArrayList<Int>()
369369

370-
@Setup
371-
fun prepare() {
372-
for (i in 0 until size) {
373-
list.add(i)
374-
}
370+
@Setup
371+
fun prepare() {
372+
for (i in 0..<size) {
373+
list.add(i)
375374
}
375+
}
376376

377-
@Benchmark
378-
fun benchmarkMethod(): Int {
379-
return list.sum()
380-
}
377+
@TearDown
378+
fun cleanup() {
379+
list.clear()
380+
}
381381

382-
@TearDown
383-
fun cleanup() {
384-
list.clear()
385-
}
382+
@Benchmark
383+
fun benchmarkMethod(): Int {
384+
return list.sum()
386385
}
386+
}
387+
```
387388

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

0 commit comments

Comments
 (0)