Skip to content

Commit 7b37e96

Browse files
committed
add WhenBenchmark
1 parent ef6875a commit 7b37e96

File tree

1 file changed

+194
-0
lines changed

1 file changed

+194
-0
lines changed
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
package microBenchmarks
2+
3+
import kotlinx.benchmark.*
4+
5+
@State(Scope.Benchmark)
6+
class WhenBenchmark {
7+
8+
private val integers = 100500..100510
9+
private val shorts = (10500..10510).map { it.toShort() }
10+
private val bytes = (100..110).map { it.toByte() }
11+
private val chars = 'a'..'k'
12+
private val strings = ('a' .. 'k').flatMap {a -> ('a' .. 'k').map { b -> "$a$b" } }
13+
14+
private val floatConst = 1.123.toFloat()
15+
private val doubleConst = 1.123456789123456
16+
17+
private val floats = (1..10).map { it * floatConst }
18+
private val doubles = (1..10).map { it * doubleConst }
19+
20+
private val integersData = mutableListOf<Int>()
21+
private val shortsData = mutableListOf<Short>()
22+
private val bytesData = mutableListOf<Byte>()
23+
private val charsData = mutableListOf<Char>()
24+
private val stringsData = mutableListOf<String>()
25+
private val floatsData = mutableListOf<Float>()
26+
private val doublesData = mutableListOf<Double>()
27+
28+
@Setup
29+
fun setup() {
30+
for (i in 1..BENCHMARK_SIZE) {
31+
charsData.add(chars.random())
32+
shortsData.add(shorts.random())
33+
bytesData.add(bytes.random())
34+
integersData.add(integers.random())
35+
stringsData.add(strings.random())
36+
floatsData.add(floats.random())
37+
doublesData.add(doubles.random())
38+
}
39+
}
40+
41+
@Benchmark
42+
fun charWhenDense(): Int {
43+
var sum = 0
44+
for (char in charsData) {
45+
when(char) {
46+
'a' -> sum += 13
47+
'c' -> sum += 91
48+
'e' -> sum += 34
49+
else -> sum += 29
50+
}
51+
}
52+
return sum
53+
}
54+
55+
@Benchmark
56+
fun charWhenSparse(): Int {
57+
var sum = 0
58+
for (char in charsData) {
59+
sum += when(char) {
60+
'a' -> 13
61+
'f' -> 34
62+
'k' -> 91
63+
else -> 29
64+
}
65+
}
66+
return sum
67+
}
68+
69+
@Benchmark
70+
fun intWhenDense(): Int {
71+
var sum = 0
72+
for (i in integersData) {
73+
sum += when(i) {
74+
100500 -> 13
75+
100502 -> 91
76+
100504 -> 34
77+
else -> 29
78+
}
79+
}
80+
return sum
81+
}
82+
83+
@Benchmark
84+
fun intWhenSparse(): Int {
85+
var sum = 0
86+
for (int in integersData) {
87+
sum += when(int) {
88+
100500 -> 13
89+
100505 -> 91
90+
100510 -> 34
91+
else -> 29
92+
}
93+
}
94+
return sum
95+
}
96+
97+
@Benchmark
98+
fun shortWhenDense(): Int {
99+
var sum = 0
100+
for (short in shortsData) {
101+
sum += when(short) {
102+
10500.toShort() -> 13
103+
10502.toShort() -> 91
104+
10504.toShort() -> 34
105+
else -> 29
106+
}
107+
}
108+
return sum
109+
}
110+
111+
@Benchmark
112+
fun shortWhenSparse(): Int {
113+
var sum = 0
114+
for (short in shortsData) {
115+
sum += when(short) {
116+
10500.toShort() -> 13
117+
10505.toShort() -> 91
118+
10510.toShort() -> 34
119+
else -> 29
120+
}
121+
}
122+
return sum
123+
}
124+
125+
@Benchmark
126+
fun byteWhenDense(): Int {
127+
var sum = 0
128+
for (byte in bytesData) {
129+
sum += when(byte) {
130+
100.toByte() -> 13
131+
102.toByte() -> 91
132+
104.toByte() -> 34
133+
else -> 29
134+
}
135+
}
136+
return sum
137+
}
138+
139+
@Benchmark
140+
fun byteWhenSparse(): Int {
141+
var sum = 0
142+
for (byte in bytesData) {
143+
sum += when(byte) {
144+
100.toByte() -> 13
145+
105.toByte() -> 91
146+
110.toByte() -> 34
147+
else -> 29
148+
}
149+
}
150+
return sum
151+
}
152+
153+
@Benchmark
154+
fun stringWhen(): Int {
155+
var sum = 0
156+
for (string in stringsData) {
157+
sum += when(string) {
158+
"aa" -> 13
159+
"bk" -> 91
160+
"fg" -> 34
161+
else -> 29
162+
}
163+
}
164+
return sum
165+
}
166+
167+
@Benchmark
168+
fun floatWhen(): Int {
169+
var sum = 0
170+
for (float in floatsData) {
171+
sum += when(float) {
172+
floatConst -> 13
173+
floatConst * 3 -> 91
174+
floatConst * 4 -> 34
175+
else -> 29
176+
}
177+
}
178+
return sum
179+
}
180+
181+
@Benchmark
182+
fun doubleWhen(): Int {
183+
var sum = 0
184+
for (double in doublesData) {
185+
sum += when(double) {
186+
doubleConst -> 13
187+
doubleConst * 3 -> 91
188+
doubleConst * 4 -> 34
189+
else -> 29
190+
}
191+
}
192+
return sum
193+
}
194+
}

0 commit comments

Comments
 (0)