You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+23-22Lines changed: 23 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -329,16 +329,15 @@ After setting up your project and configuring targets, you can start writing ben
329
329
330
330
```kotlin
331
331
privateval size =10
332
-
333
-
privateval list =ArrayList()
332
+
privateval list =ArrayList<Int>()
334
333
```
335
334
336
335
3. **InitializeResources**: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.
337
336
338
337
```kotlin
339
338
@Setup
340
339
funprepare() {
341
-
for (i in0 until size) {
340
+
for (i in0..<size) {
342
341
list.add(i)
343
342
}
344
343
}
@@ -349,7 +348,7 @@ After setting up your project and configuring targets, you can start writing ben
349
348
}
350
349
```
351
350
352
-
4. **DefineBenchmarkMethod**:Next, create methods that you would like to be benchmarked within thisclassand annotate them with `@Benchmark`.
351
+
4. **DefineBenchmarkMethods**:Next, create methods that you would like to be benchmarked within thisclassand annotate them with `@Benchmark`.
353
352
354
353
```kotlin
355
354
@Benchmark
@@ -360,30 +359,32 @@ After setting up your project and configuring targets, you can start writing ben
360
359
361
360
Yourfinal benchmark classwill look something like this:
362
361
363
-
@State(Scope.Benchmark)
364
-
classMyBenchmark {
365
-
366
-
privateval size =10
362
+
```kotlin
363
+
import kotlinx.benchmark.*
367
364
368
-
privateval list =ArrayList()
365
+
@State(Scope.Benchmark)
366
+
classMyBenchmark {
367
+
privateval size =10
368
+
privateval list =ArrayList<Int>()
369
369
370
-
@Setup
371
-
funprepare() {
372
-
for (i in0 until size) {
373
-
list.add(i)
374
-
}
370
+
@Setup
371
+
funprepare() {
372
+
for (i in0..<size) {
373
+
list.add(i)
375
374
}
375
+
}
376
376
377
-
@Benchmark
378
-
funbenchmarkMethod(): Int {
379
-
returnlist.sum()
380
-
}
377
+
@TearDown
378
+
funcleanup() {
379
+
list.clear()
380
+
}
381
381
382
-
@TearDown
383
-
funcleanup() {
384
-
list.clear()
385
-
}
382
+
@Benchmark
383
+
funbenchmarkMethod(): Int {
384
+
return list.sum()
386
385
}
386
+
}
387
+
```
387
388
388
389
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.
0 commit comments