代码拉取完成,页面将自动刷新
# This file is a part of Julia. License is MIT: https://julialang.org/license
module BinaryTreeMutable
# Adopted from
# https://benchmarksgame-team.pages.debian.net/benchmarksgame/description/binarytrees.html#binarytrees
using Base.Threads
using Printf
mutable struct Node
l::Union{Nothing, Node}
r::Union{Nothing, Node}
end
function make(n::Int)
return n === 0 ? Node(nothing, nothing) : Node(make(n-1), make(n-1))
end
function check(node::Node)
return 1 + (node.l === nothing ? 0 : check(node.l) + check(node.r))
end
function binary_trees(io, n::Int)
@printf io "stretch tree of depth %jd\t check: %jd\n" n+1 check(make(n+1))
long_tree = make(n)
minDepth = 4
resultSize = div((n - minDepth), 2) + 1
results = Vector{String}(undef, resultSize)
Threads.@threads for depth in minDepth:2:n
c = 0
niter = 1 << (n - depth + minDepth)
for _ in 1:niter
c += check(make(depth))
end
index = div((depth - minDepth),2) + 1
results[index] = @sprintf "%jd\t trees of depth %jd\t check: %jd\n" niter depth c
end
for i in results
write(io, i)
end
@printf io "long lived tree of depth %jd\t check: %jd\n" n check(long_tree)
end
end #module
using .BinaryTreeMutable
# Memory usage is 466MB
BinaryTreeMutable.binary_trees(devnull, 16)
GC.gc()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。