The doc page says that
empty!
is nearly costless (and O(1)) for types that support this kind of preallocation.
But the code of empty!
and _deleteend!
show that it’s in O(n) complexity. The _unsetindex!
in iteration body seems to do some gc reference counting job. However, is this necessary for a primitive type like Float64
?
I also did some simple benchmark:
julia> a = ones(100000); @time empty!(a);
0.000010 seconds
julia> a = ones(1000000); @time empty!(a);
0.000042 seconds
julia> a = ones(10000000); @time empty!(a);
0.000516 seconds
julia> a = ones(100000000); @time empty!(a);
0.003783 seconds
It does look like in O(n) (linear time complexity).
I consider that the only thing empty!(::Vector{Float64})
should do is to reset the “end pointer” to the first element and the length to 0, which is O(1).
Did I miss something? How to reuse a Vector
efficiently?