following my recursive example, now include the test of TypeSortedCollections
:
import TypeSortedCollections
const TSC = TypeSortedCollections.TypeSortedCollection
tsc = TSC(heter, true)
function h(v::TSC)
total = 0.0
let total = total
foreach(function fun(a) total += a.x end, v)
# println(total)
end
end
then, f()
(that directly accesses heterogeneous elements) and g()
(that uses recursion) are slightly modified as follows:
function f(a::Vector{MyParamType} )
total = 0.0
for i in 1:length(a)
total += a[i].x
end
# println(total)
end
function g(b::Recursive)
total = Float64(b.part1.x)
if b.part2 isa MyParamType
return total + b.part2.x
else
return total + g(b.part2)
end
# println(total)
end
finally, their run time:
julia> @btime f(heter)
159.109 ns (6 allocations: 96 bytes)
julia> @btime g(recur)
19.245 ns (1 allocation: 16 bytes)
julia> @btime h(tsc)
131.617 ns (8 allocations: 128 bytes)
note that only f(heter)
causes warntype.
now, in this example, recursion is much faster. While TypeSortedCollections
helps to avoid warntype, seems that it has a lot of memory allocation overhead.