Skip to content

Commit 338826b

Browse files
committed
added my benchmarks to show the best case performance increase
1 parent da5bd0e commit 338826b

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import examples/benchmarks/runner
2+
3+
record Vec4(a: Int, b: Int, c: Int, d: Int)
4+
5+
record Matrix4(row1: Vec4, row2: Vec4, row3: Vec4, row4: Vec4)
6+
7+
record Vec3(a: Int, b: Int, c: Int)
8+
record Matrix3(row1: Vec3, row2: Vec3, row3: Vec3)
9+
10+
def det3(m: Matrix3): Int = {
11+
m.row1.a * (m.row2.b * m.row3.c - m.row2.c * m.row3.b) -
12+
m.row1.b * (m.row2.a * m.row3.c - m.row2.c * m.row3.a) +
13+
m.row1.c * (m.row2.a * m.row3.b - m.row2.b * m.row3.a)
14+
}
15+
16+
def det4(m: Matrix4): Int = {
17+
val c1 = m.row1.a * det3(Matrix3(
18+
Vec3(m.row2.b, m.row2.c, m.row2.d),
19+
Vec3(m.row3.b, m.row3.c, m.row3.d),
20+
Vec3(m.row4.b, m.row4.c, m.row4.d)
21+
))
22+
val c2 = m.row1.b * det3(Matrix3(
23+
Vec3(m.row2.a, m.row2.c, m.row2.d),
24+
Vec3(m.row3.a, m.row3.c, m.row3.d),
25+
Vec3(m.row4.a, m.row4.c, m.row4.d)
26+
))
27+
val c3 = m.row1.c * det3(Matrix3(
28+
Vec3(m.row2.a, m.row2.b, m.row2.d),
29+
Vec3(m.row3.a, m.row3.b, m.row3.d),
30+
Vec3(m.row4.a, m.row4.b, m.row4.d)
31+
))
32+
val c4 = m.row1.d * det3(Matrix3(
33+
Vec3(m.row2.a, m.row2.b, m.row2.c),
34+
Vec3(m.row3.a, m.row3.b, m.row3.c),
35+
Vec3(m.row4.a, m.row4.b, m.row4.c)
36+
))
37+
c1 - c2 + c3 - c4
38+
}
39+
40+
def runBenchmark(n: Int): Int = {
41+
def loop(i: Int, acc: Int): Int = {
42+
if (i <= 0) { acc }
43+
else {
44+
val m = Matrix4(
45+
Vec4(i, i + 1, i + 2, i + 3),
46+
Vec4(i + 4, i + 5, i + 6, i + 7),
47+
Vec4(i + 8, i + 9, i + 10, i + 11),
48+
Vec4(i + 12, i + 13, i + 14, i + 15)
49+
)
50+
loop(i - 1, acc + det4(m))
51+
}
52+
}
53+
loop(n, 0)
54+
}
55+
56+
def main() = benchmark(1000000){ n => runBenchmark(n) }
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import examples/benchmarks/runner
2+
3+
record Point(x: Int, y: Int)
4+
5+
def add(p: Point, depth: Int): Int = {
6+
if (depth <= 0) { p.x + p.y }
7+
else { add(Point(p.y, p.x), depth - 1) }
8+
}
9+
10+
def runBenchmark(n: Int): Int = {
11+
def loop(i: Int, acc: Int): Int = {
12+
if (i <= 0) { acc }
13+
else { loop(i - 1, acc + add(Point(i, i + 1), 2)) }
14+
}
15+
loop(n, 0)
16+
}
17+
18+
def main() = benchmark(250000000){ n => runBenchmark(n) }

0 commit comments

Comments
 (0)