Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Algol68
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C with Coccinelle
C++ with Coccinelle
C++ (Circle)
CIRCT
Clean
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Mojo
Nim
Numba
Nix
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
PTX
Python
Racket
Raku
Ruby
Rust
Sail
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
Triton
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Zig
Javascript
GIMPLE
Ygen
sway
swift source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
aarch64 swiftc 6.0.3
aarch64 swiftc 6.1
aarch64 swiftc 6.2
x86-64 swiftc 3.1.1
x86-64 swiftc 4.0.2
x86-64 swiftc 4.0.3
x86-64 swiftc 4.1
x86-64 swiftc 4.1.1
x86-64 swiftc 4.1.2
x86-64 swiftc 4.2
x86-64 swiftc 5.0
x86-64 swiftc 5.1
x86-64 swiftc 5.10
x86-64 swiftc 5.2
x86-64 swiftc 5.3
x86-64 swiftc 5.4
x86-64 swiftc 5.5
x86-64 swiftc 5.6
x86-64 swiftc 5.7
x86-64 swiftc 5.8
x86-64 swiftc 5.9
x86-64 swiftc 6.0.3
x86-64 swiftc 6.1
x86-64 swiftc 6.2
x86-64 swiftc devsnapshot
x86-64 swiftc nightly
Options
Source code
// adapted from original implementation submitted by Isaac Gouy: // https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/binarytrees-swift-8.html final class Tree { private var left, right: Tree? init(_ left: Tree?, _ right: Tree?) { self.left = left self.right = right } static func with(_ depth: Int) -> Tree { return (depth == 0) ? Tree(nil, nil) : Tree(with(depth - 1), with(depth - 1)) } borrowing func nodeCount() -> Int { return (left == nil) ? 1 : 1 + left!.nodeCount() + right!.nodeCount() } // Honestly, we should just go ahead and allow mutating methods // directly on classes. It's niche, but we can already implement // them with protocol extensions, and we allow the other two // ownership specifiers. consuming func clear() { // this deletes all of the nodes in the tree, since we never // escaped any of them. } } func main(_ n: Int) { let minDepth = 4 let maxDepth = minDepth + 2 > n ? minDepth + 2 : n let stretchDepth = maxDepth + 1 stretch(stretchDepth) let longLivedTree = Tree.with(maxDepth) for depth in stride(from: minDepth, to: stretchDepth, by: 2) { let iterations = 1 << (maxDepth - depth + minDepth) var sum = 0 for _ in 1...iterations { sum += count(depth) } print("\(iterations)\t trees of depth \(depth)\t check: \(sum)") } let count = longLivedTree.nodeCount() longLivedTree.clear() print("long lived tree of depth \(maxDepth)\t check: \(count)") } func stretch(_ depth: Int) { print("stretch tree of depth \(depth)\t check: \(count(depth))") } func count(_ depth: Int) -> Int { let t = Tree.with(depth) let c = t.nodeCount() t.clear() return c } do { let n = if CommandLine.argc > 1 { Int(CommandLine.arguments[1])! } else { 10 } main(n) }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Compiler Explorer Shop
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
Statistics
Changelog
Version tree