Consider the following piece of code, a toy model of a larger code:
immutable Element{T}
a::T
end
type Mesh{E}
elsvec::Vector{E}
end
immutable Elements{I}
els::I
end
elements(m::Mesh) = Elements(m.elsvec)
Base.start(e::Elements) = 1
Base.next(e::Elements, i::Int) = e.els[i], i+1
Base.done(e::Elements, i::Int) = length(e.els) == i
Base.eltype{I}(::Type{Elements{I}}) = eltype(I)
const v = Element[Element(i) for i = 1:1000000]
const m = Mesh(v)
function test(m)
I = 1
for (j, e) in enumerate(elements(m))
I += j
end
I
end
function test2(m)
I = 1
j = 1
for e in elements(m)
I += j
j += 1
end
I
end
@time test(m)
@time test(m)
@time test(m)
@time test2(m)
@time test2(m)
@time test2(m)
Why does test results is such a huge memory allocation?
Thanks