From 522645e1abb33fd43bf6b79e77d0fd98d94e895d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Wed, 27 Nov 2024 15:40:28 -0800 Subject: [PATCH 1/2] add some benchmark programs --- benchmarks/binarytrees.cdc | 82 ++++++++++++++++++++++++++++++++ benchmarks/fannkuch.cdc | 95 ++++++++++++++++++++++++++++++++++++++ benchmarks/fib_dynamic.cdc | 21 +++++++++ 3 files changed, 198 insertions(+) create mode 100644 benchmarks/binarytrees.cdc create mode 100644 benchmarks/fannkuch.cdc create mode 100644 benchmarks/fib_dynamic.cdc diff --git a/benchmarks/binarytrees.cdc b/benchmarks/binarytrees.cdc new file mode 100644 index 000000000..bdfa6a028 --- /dev/null +++ b/benchmarks/binarytrees.cdc @@ -0,0 +1,82 @@ + +access(all) +struct Tree { + access(all) + var left: Tree? + + access(all) + var right: Tree? + + init(left: Tree?, right: Tree?) { + self.left = left + self.right = right + } + + access(all) + fun nodeCount(): Int { + return 1 + + (self.left?.nodeCount() ?? 0) + + (self.right?.nodeCount() ?? 0) + } + + access(all) + fun clear() { + if (self.left != nil) { + self.left?.clear() + self.left = nil + self.right?.clear() + self.right = nil + } + } +} + +access(all) +fun newTree(depth: Int): Tree { + if depth == 0 { + return Tree(left: nil, right: nil) + } + return Tree( + left: newTree(depth: depth - 1), + right: newTree(depth: depth - 1) + ) +} + +access(all) +fun stretch(_ depth: Int) { + log("stretch tree of depth \(depth), check: \(count(depth))") +} + +access(all) +fun count(_ depth: Int): Int { + let t = newTree(depth: depth) + let c = t.nodeCount() + t.clear() + return c +} + +access(all) +fun run(_ n: Int) { + let minDepth = 4 + let maxDepth = minDepth + 2 > n ? minDepth + 2 : n + let stretchDepth = maxDepth + 1 + + stretch(stretchDepth) + let longLivedTree = newTree(depth: maxDepth) + + for depth in InclusiveRange(minDepth, maxDepth, step: 2) { + let iterations = 1 << (maxDepth - depth + minDepth) + var sum = 0 + for _ in InclusiveRange(1, iterations, step: 1) { + sum = sum + count(depth) + } + log("\(iterations), trees of depth \(depth), check: \(sum)") + } + let count = longLivedTree.nodeCount() + longLivedTree.clear() + log("long lived tree of depth \(maxDepth), check: \(count)") +} + +access(all) +fun main() { + run(10) +} diff --git a/benchmarks/fannkuch.cdc b/benchmarks/fannkuch.cdc new file mode 100644 index 000000000..0e4d1e2ac --- /dev/null +++ b/benchmarks/fannkuch.cdc @@ -0,0 +1,95 @@ +access(all) +fun newArray(repeating value: Int, count: Int): [Int] { + let array: [Int] = [] + for _ in InclusiveRange(0, count-1) { + array.append(value) + } + return array +} + +access(all) +fun fannkuch(_ n: Int): Int { + let perm = newArray(repeating: 0, count: n) + let count = newArray(repeating: 0, count: n) + let perm1 = newArray(repeating: 0, count: n) + + for j in InclusiveRange(0, n-1) { + perm1[j] = j + } + + var f = 0 + var i = 0 + var k = 0 + var r = 0 + var flips = 0 + var nperm = 0 + var checksum = 0 + + r = n + while r > 0 { + i = 0 + while r != 1 { + count[r-1] = r + r = r - 1 + } + while i < n { + perm[i] = perm1[i] + i = i + 1 + } + + // Count flips and update max and checksum + f = 0 + k = perm[0] + while k != 0 { + i = 0 + while 2*i < k { + let t = perm[i] + perm[i] = perm[k-i] + perm[k-i] = t + i = i + 1 + } + k = perm[0] + f = f + 1 + } + if f > flips { + flips = f + } + + if (nperm & 0x1) == 0 { + checksum = checksum + f + } else { + checksum = checksum - f + } + + // Use incremental change to generate another permutation + var more = true + while more { + if r == n { + log(checksum) + return flips + } + let p0 = perm1[0] + i = 0 + while i < r { + let j = i+1 + perm1[i] = perm1[j] + i = j + } + perm1[r] = p0 + + count[r] = count[r] - 1 + if count[r] > 0 { + more = false + } else { + r = r + 1 + } + } + nperm = nperm + 1 + } + return flips +} + +access(all) +fun main() { + assert(fannkuch(7) == 16) +} diff --git a/benchmarks/fib_dynamic.cdc b/benchmarks/fib_dynamic.cdc new file mode 100644 index 000000000..ac608f3c4 --- /dev/null +++ b/benchmarks/fib_dynamic.cdc @@ -0,0 +1,21 @@ +access(all) +fun fib(_ n: Int): Int { + if n == 0 { + return 0 + } + + let f = [0, 1] + + var i = 2 + while i <= n { + f.append(f[i - 1] + f[i - 2]) + i = i + 1 + } + + return f[n] +} + +access(all) +fun main() { + assert(fib(14) == 377) +} \ No newline at end of file From b3bc5c1aff1f025b54c85791a0fd12f681df9e3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Wed, 27 Nov 2024 15:55:23 -0800 Subject: [PATCH 2/2] add iterative and recursive fibonacci functions --- benchmarks/fib_dynamic.cdc | 4 ++-- benchmarks/fib_iterative.cdc | 19 +++++++++++++++++++ benchmarks/fib_recursive.cdc | 12 ++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 benchmarks/fib_iterative.cdc create mode 100644 benchmarks/fib_recursive.cdc diff --git a/benchmarks/fib_dynamic.cdc b/benchmarks/fib_dynamic.cdc index ac608f3c4..55248afa1 100644 --- a/benchmarks/fib_dynamic.cdc +++ b/benchmarks/fib_dynamic.cdc @@ -17,5 +17,5 @@ fun fib(_ n: Int): Int { access(all) fun main() { - assert(fib(14) == 377) -} \ No newline at end of file + assert(fib(23) == 28657) +} diff --git a/benchmarks/fib_iterative.cdc b/benchmarks/fib_iterative.cdc new file mode 100644 index 000000000..a7a162270 --- /dev/null +++ b/benchmarks/fib_iterative.cdc @@ -0,0 +1,19 @@ +access(all) +fun fib(_ n: Int): Int { + var fib1 = 1 + var fib2 = 1 + var fibonacci = fib1 + var i = 2 + while i < n { + fibonacci = fib1 + fib2 + fib1 = fib2 + fib2 = fibonacci + i = i + 1 + } + return fibonacci +} + +access(all) +fun main() { + assert(fib(23) == 28657) +} diff --git a/benchmarks/fib_recursive.cdc b/benchmarks/fib_recursive.cdc new file mode 100644 index 000000000..5b06a75a4 --- /dev/null +++ b/benchmarks/fib_recursive.cdc @@ -0,0 +1,12 @@ +access(all) +fun fib(_ n: Int): Int { + if n < 2 { + return n + } + return fib(n - 1) + fib(n - 2) +} + +access(all) +fun main() { + assert(fib(23) == 28657) +}