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
+24-23Lines changed: 24 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -315,7 +315,8 @@ Note: Kotlin/WASM is an experimental compilation target for Kotlin. It may be dr
315
315
316
316
### WritingBenchmarks
317
317
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:
319
320
320
321
1. **Create Benchmark Class**: Create a class in your source set where you'd like to add the benchmark. Annotatethisclasswith `@State(Scope.Benchmark)`.
321
322
@@ -326,12 +327,11 @@ After setting up your project and configuring targets, you can start writing ben
326
327
}
327
328
```
328
329
329
-
2. **Set up ParametersandVariables**:Define variables needed for the benchmark.
330
+
2. **Set up Variables**:Define variables needed for the benchmark.
330
331
331
332
```kotlin
332
-
var param:Int=10
333
-
334
-
privatevar list:MutableList<Int> =ArrayList()
333
+
privateval size =10
334
+
privateval list =ArrayList<Int>()
335
335
```
336
336
337
337
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.
@@ -361,30 +361,31 @@ After setting up your project and configuring targets, you can start writing ben
361
361
362
362
Yourfinal benchmark classwill look something like this:
363
363
364
-
@State(Scope.Benchmark)
365
-
classMyBenchmark {
366
-
367
-
var param:Int=10
364
+
```kotlin
365
+
@State(Scope.Benchmark)
366
+
classMyBenchmark {
368
367
369
-
privatevar list:MutableList<Int> =ArrayList()
368
+
privateval size =10
369
+
privateval list =ArrayList<Int>()
370
370
371
-
@Setup
372
-
funprepare() {
373
-
for (i in0 until size) {
374
-
list.add(i)
375
-
}
371
+
@Setup
372
+
funprepare() {
373
+
for (i in0 until size) {
374
+
list.add(i)
376
375
}
376
+
}
377
377
378
-
@Benchmark
379
-
funbenchmarkMethod(): Int {
380
-
return list.sum()
381
-
}
378
+
@Benchmark
379
+
funbenchmarkMethod(): Int {
380
+
return list.sum()
381
+
}
382
382
383
-
@TearDown
384
-
funcleanup() {
385
-
list.clear()
386
-
}
383
+
@TearDown
384
+
funcleanup() {
385
+
list.clear()
387
386
}
387
+
}
388
+
```
388
389
389
390
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