diff --git a/bench/algorithm/binarytrees/1.swift b/bench/algorithm/binarytrees/1.swift new file mode 100644 index 000000000..60b589df5 --- /dev/null +++ b/bench/algorithm/binarytrees/1.swift @@ -0,0 +1,48 @@ +import Foundation + +let minDepth = UInt32(4) +let maxDepth = CommandLine.argc > 1 ? max(UInt32(CommandLine.arguments[1]) ?? 0, minDepth + 2) : 10 +let stretchDepth = maxDepth + 1 +let check = Tree(depth: stretchDepth).check +print("stretch tree of depth \(stretchDepth)\t check: \(check)") + +let longLivingTree = Tree(depth: maxDepth) + +for halfDepth in (minDepth / 2)..<(maxDepth / 2 + 1) { + let depth = halfDepth * 2 + let iterations = UInt32(1 << (maxDepth - depth + minDepth)) + var chk: UInt32 = 0 + for _ in 1...iterations { + chk += Tree(depth: depth).check + } + print("\(iterations)\t trees of depth \(depth)\t check: \(chk)") +} + +print("long lived tree of depth \(maxDepth)\t check: \(longLivingTree.check)") + +indirect enum Tree { + case Empty + case Node(left: Tree, right: Tree) + + init(depth: UInt32) { + if depth > 0 { + self = .Node(left: Tree(depth: depth - 1), right: Tree(depth: depth - 1)) + } else { + self = .Node(left: .Empty, right: .Empty) + } + } + + var check: UInt32 { + switch self { + case .Node(let left, let right): + switch (left, right) { + case (.Empty, .Empty): + return 1 + default: + return 1 + left.check + right.check + } + case .Empty: + return 1 + } + } +} diff --git a/bench/bench_swift.yaml b/bench/bench_swift.yaml index 9d32da677..f4154478b 100644 --- a/bench/bench_swift.yaml +++ b/bench/bench_swift.yaml @@ -3,6 +3,9 @@ problems: - name: helloworld source: - 1.swift + - name: binarytrees + source: + - 1.swift - name: nbody source: - 7.swift