Skip to content

Commit b4f9190

Browse files
committed
Added ArithmeticBenchmark
1 parent 742ca76 commit b4f9190

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2025 JetBrains s.r.o.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package microBenchmarks
18+
19+
import kotlinx.benchmark.*
20+
21+
@State(Scope.Benchmark)
22+
class ArithmeticBenchmark {
23+
@Benchmark
24+
fun division(): Int {
25+
var i = 1
26+
var j = 1
27+
while (i < BENCHMARK_SIZE) {
28+
j += (i shl 1234) / i / j / i / j / i / j
29+
i++
30+
}
31+
return j
32+
}
33+
34+
@Benchmark
35+
fun division_constant(): Int {
36+
var i = 1
37+
var j = 1
38+
while (i < BENCHMARK_SIZE) {
39+
j += (i shl 1234) / 42 / j / 42 / j / 42 / j
40+
i++
41+
}
42+
return j
43+
}
44+
45+
@Benchmark
46+
fun reminder(): Int {
47+
var i = 1
48+
var j = 1
49+
while (i < BENCHMARK_SIZE) {
50+
j += (i shl 1234) % i % j % i % j % i % j
51+
i++
52+
}
53+
return j
54+
}
55+
56+
@Benchmark
57+
fun reminder_constant(): Int {
58+
var i = 1
59+
var j = 1
60+
while (i < BENCHMARK_SIZE) {
61+
j += (i shl 1234) % 42 % j % 42 % j % 42 % j
62+
i++
63+
}
64+
return j
65+
}
66+
}

0 commit comments

Comments
 (0)