From a3b9738d5d67140526c7617eb2d9c11268b62e67 Mon Sep 17 00:00:00 2001 From: acefsm Date: Sun, 12 Feb 2023 03:36:31 +0300 Subject: [PATCH 1/8] binarytrees benchmark for swift --- bench/algorithm/binarytrees/1.swift | 68 +++++++++++++++++++++++++++++ bench/bench_swift.yaml | 3 ++ 2 files changed, 71 insertions(+) create mode 100644 bench/algorithm/binarytrees/1.swift diff --git a/bench/algorithm/binarytrees/1.swift b/bench/algorithm/binarytrees/1.swift new file mode 100644 index 000000000..007352820 --- /dev/null +++ b/bench/algorithm/binarytrees/1.swift @@ -0,0 +1,68 @@ +import Dispatch +import Foundation + +let n: UInt32 + +if CommandLine.argc > 1 { + n = UInt32(CommandLine.arguments[1]) ?? UInt32(10) +} else { + n = 10 +} + +let minDepth = UInt32(4) +let maxDepth = (n > minDepth + 2) ? n : minDepth + 2 +let depth = maxDepth + 1 +let check = Foo(depth: depth).check() +print("stretch tree of depth \(maxDepth+1)\t check: \(check)") + +var messages = [(UInt32, String)]() +let longLivingTree = Foo(depth: maxDepth) +await withTaskGroup(of: (UInt32, String).self) { group in + for d in stride(from: minDepth, to: maxDepth, by: 2) { + group.addTask { + let iterations = UInt32(1 << (maxDepth - d + minDepth)) + var chk: UInt32 = 0 + for _ in 1...iterations { + chk += Foo(depth: d).check() + } + return (d, "\(iterations)\t trees of depth \(d)\t check: \(chk)") + } + } + + for await msg in group { + messages.append(msg) + } + +} +for msg in messages.sorted(by: { $0.0 < $1.0 }) { + print(msg.1) +} +print("long lived tree of depth \(maxDepth)\t check: \(longLivingTree.check())") + + +indirect enum Foo { + case Empty + case Node(left: Foo, right: Foo) + + init(depth: UInt32) { + if depth > 0 { + self = .Node(left: Foo(depth: depth - 1), right: Foo(depth: depth - 1)) + } else { + self = .Node(left: .Empty, right: .Empty) + } + } + + func 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 From be2bcb9004c65aa21d5deb12dd273f59fca188e9 Mon Sep 17 00:00:00 2001 From: acefsm Date: Sun, 12 Feb 2023 03:40:26 +0300 Subject: [PATCH 2/8] fix naming --- bench/algorithm/binarytrees/1.swift | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bench/algorithm/binarytrees/1.swift b/bench/algorithm/binarytrees/1.swift index 007352820..98f560b6e 100644 --- a/bench/algorithm/binarytrees/1.swift +++ b/bench/algorithm/binarytrees/1.swift @@ -12,18 +12,18 @@ if CommandLine.argc > 1 { let minDepth = UInt32(4) let maxDepth = (n > minDepth + 2) ? n : minDepth + 2 let depth = maxDepth + 1 -let check = Foo(depth: depth).check() +let check = Tree(depth: depth).check() print("stretch tree of depth \(maxDepth+1)\t check: \(check)") var messages = [(UInt32, String)]() -let longLivingTree = Foo(depth: maxDepth) +let longLivingTree = Tree(depth: maxDepth) await withTaskGroup(of: (UInt32, String).self) { group in for d in stride(from: minDepth, to: maxDepth, by: 2) { group.addTask { let iterations = UInt32(1 << (maxDepth - d + minDepth)) var chk: UInt32 = 0 for _ in 1...iterations { - chk += Foo(depth: d).check() + chk += Tree(depth: d).check() } return (d, "\(iterations)\t trees of depth \(d)\t check: \(chk)") } @@ -40,13 +40,13 @@ for msg in messages.sorted(by: { $0.0 < $1.0 }) { print("long lived tree of depth \(maxDepth)\t check: \(longLivingTree.check())") -indirect enum Foo { +indirect enum Tree { case Empty - case Node(left: Foo, right: Foo) + case Node(left: Tree, right: Tree) init(depth: UInt32) { if depth > 0 { - self = .Node(left: Foo(depth: depth - 1), right: Foo(depth: depth - 1)) + self = .Node(left: Tree(depth: depth - 1), right: Tree(depth: depth - 1)) } else { self = .Node(left: .Empty, right: .Empty) } From 785595b215d9fa6ef958f10b716e1f149dbdeae9 Mon Sep 17 00:00:00 2001 From: acefsm Date: Sun, 12 Feb 2023 03:44:55 +0300 Subject: [PATCH 3/8] function to get property --- bench/algorithm/binarytrees/1.swift | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bench/algorithm/binarytrees/1.swift b/bench/algorithm/binarytrees/1.swift index 98f560b6e..1ed5ce73f 100644 --- a/bench/algorithm/binarytrees/1.swift +++ b/bench/algorithm/binarytrees/1.swift @@ -12,7 +12,7 @@ if CommandLine.argc > 1 { let minDepth = UInt32(4) let maxDepth = (n > minDepth + 2) ? n : minDepth + 2 let depth = maxDepth + 1 -let check = Tree(depth: depth).check() +let check = Tree(depth: depth).check print("stretch tree of depth \(maxDepth+1)\t check: \(check)") var messages = [(UInt32, String)]() @@ -23,7 +23,7 @@ await withTaskGroup(of: (UInt32, String).self) { group in let iterations = UInt32(1 << (maxDepth - d + minDepth)) var chk: UInt32 = 0 for _ in 1...iterations { - chk += Tree(depth: d).check() + chk += Tree(depth: d).check } return (d, "\(iterations)\t trees of depth \(d)\t check: \(chk)") } @@ -37,7 +37,7 @@ await withTaskGroup(of: (UInt32, String).self) { group in for msg in messages.sorted(by: { $0.0 < $1.0 }) { print(msg.1) } -print("long lived tree of depth \(maxDepth)\t check: \(longLivingTree.check())") +print("long lived tree of depth \(maxDepth)\t check: \(longLivingTree.check)") indirect enum Tree { @@ -52,14 +52,14 @@ indirect enum Tree { } } - func check() -> UInt32 { + 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() + return 1 + left.check + right.check } case .Empty: return 1 From e863b207f3eaddb9d21da6edd15bc8965a130604 Mon Sep 17 00:00:00 2001 From: acefsm Date: Sun, 12 Feb 2023 03:47:40 +0300 Subject: [PATCH 4/8] clean code --- bench/algorithm/binarytrees/1.swift | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/bench/algorithm/binarytrees/1.swift b/bench/algorithm/binarytrees/1.swift index 1ed5ce73f..f328f4760 100644 --- a/bench/algorithm/binarytrees/1.swift +++ b/bench/algorithm/binarytrees/1.swift @@ -1,12 +1,10 @@ import Dispatch import Foundation -let n: UInt32 +let n = UInt32(10) if CommandLine.argc > 1 { n = UInt32(CommandLine.arguments[1]) ?? UInt32(10) -} else { - n = 10 } let minDepth = UInt32(4) From c1f6741e9faba8602ab987edb7596d557784c1e1 Mon Sep 17 00:00:00 2001 From: acefsm Date: Sun, 12 Feb 2023 03:48:58 +0300 Subject: [PATCH 5/8] clean code --- bench/algorithm/binarytrees/1.swift | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/bench/algorithm/binarytrees/1.swift b/bench/algorithm/binarytrees/1.swift index f328f4760..c19e01114 100644 --- a/bench/algorithm/binarytrees/1.swift +++ b/bench/algorithm/binarytrees/1.swift @@ -1,7 +1,6 @@ -import Dispatch import Foundation -let n = UInt32(10) +var n = UInt32(10) if CommandLine.argc > 1 { n = UInt32(CommandLine.arguments[1]) ?? UInt32(10) @@ -26,22 +25,21 @@ await withTaskGroup(of: (UInt32, String).self) { group in return (d, "\(iterations)\t trees of depth \(d)\t check: \(chk)") } } - + for await msg in group { messages.append(msg) } - + } for msg in messages.sorted(by: { $0.0 < $1.0 }) { print(msg.1) } 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)) @@ -49,7 +47,7 @@ indirect enum Tree { self = .Node(left: .Empty, right: .Empty) } } - + var check: UInt32 { switch self { case .Node(let left, let right): From 00ce9cff69669ebd7f94dfa54d04da0f5286d802 Mon Sep 17 00:00:00 2001 From: acefsm Date: Sun, 12 Feb 2023 03:50:27 +0300 Subject: [PATCH 6/8] clean code --- bench/algorithm/binarytrees/1.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bench/algorithm/binarytrees/1.swift b/bench/algorithm/binarytrees/1.swift index c19e01114..a992cb5f1 100644 --- a/bench/algorithm/binarytrees/1.swift +++ b/bench/algorithm/binarytrees/1.swift @@ -29,8 +29,8 @@ await withTaskGroup(of: (UInt32, String).self) { group in for await msg in group { messages.append(msg) } - } + for msg in messages.sorted(by: { $0.0 < $1.0 }) { print(msg.1) } From 3b12323e93e992d839d66a28e1eb898c08c4ffdb Mon Sep 17 00:00:00 2001 From: acefsm Date: Thu, 23 Feb 2023 00:46:22 +0300 Subject: [PATCH 7/8] single-threaded solution --- bench/algorithm/binarytrees/1.swift | 31 ++++++++++------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/bench/algorithm/binarytrees/1.swift b/bench/algorithm/binarytrees/1.swift index a992cb5f1..6b03e1120 100644 --- a/bench/algorithm/binarytrees/1.swift +++ b/bench/algorithm/binarytrees/1.swift @@ -8,32 +8,21 @@ if CommandLine.argc > 1 { let minDepth = UInt32(4) let maxDepth = (n > minDepth + 2) ? n : minDepth + 2 -let depth = maxDepth + 1 -let check = Tree(depth: depth).check -print("stretch tree of depth \(maxDepth+1)\t check: \(check)") +let stretchDepth = maxDepth + 1 +let check = Tree(depth: stretchDepth).check +print("stretch tree of depth \(stretchDepth)\t check: \(check)") -var messages = [(UInt32, String)]() let longLivingTree = Tree(depth: maxDepth) -await withTaskGroup(of: (UInt32, String).self) { group in - for d in stride(from: minDepth, to: maxDepth, by: 2) { - group.addTask { - let iterations = UInt32(1 << (maxDepth - d + minDepth)) - var chk: UInt32 = 0 - for _ in 1...iterations { - chk += Tree(depth: d).check - } - return (d, "\(iterations)\t trees of depth \(d)\t check: \(chk)") - } - } - - for await msg in group { - messages.append(msg) + +for depth in stride(from: minDepth, to: maxDepth, by: 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)") } -for msg in messages.sorted(by: { $0.0 < $1.0 }) { - print(msg.1) -} print("long lived tree of depth \(maxDepth)\t check: \(longLivingTree.check)") indirect enum Tree { From 70df07c49d09b76deb2e76c96644064c0b3111fc Mon Sep 17 00:00:00 2001 From: acefsm Date: Mon, 27 Feb 2023 03:00:22 +0300 Subject: [PATCH 8/8] fixed algo --- bench/algorithm/binarytrees/1.swift | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/bench/algorithm/binarytrees/1.swift b/bench/algorithm/binarytrees/1.swift index 6b03e1120..60b589df5 100644 --- a/bench/algorithm/binarytrees/1.swift +++ b/bench/algorithm/binarytrees/1.swift @@ -1,20 +1,15 @@ import Foundation -var n = UInt32(10) - -if CommandLine.argc > 1 { - n = UInt32(CommandLine.arguments[1]) ?? UInt32(10) -} - let minDepth = UInt32(4) -let maxDepth = (n > minDepth + 2) ? n : minDepth + 2 +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 depth in stride(from: minDepth, to: maxDepth, by: 2) { +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 {