Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions bench/algorithm/binarytrees/1.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
}
3 changes: 3 additions & 0 deletions bench/bench_swift.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ problems:
- name: helloworld
source:
- 1.swift
- name: binarytrees
source:
- 1.swift
- name: nbody
source:
- 7.swift
Expand Down