PMA is a vector, iterating over its element is the same as doing :
function getsum2(vector::PackedMemoryArray)
vector_sum = 0.0
id_sum = 0
for elem in vector.array
if elem !== nothing
id, value = elem
vector_sum += value
id_sum += id.uid
end
end
return vector_sum
end
I timed the loop over a Vector{Tuple{Id, Float64}}, PMA, and Vector{Union{Nothing, Tuple{Id, Float64}}
and indeed, the loop over the Vector{Tuple{Int, Float64}} is the fastest.
EDIT : test on julia1.4
1.218 μs (0 allocations: 0 bytes). #vector
11.730 μs (0 allocations: 0 bytes). #pma (getsum)
10.820 μs (0 allocations: 0 bytes). #pma (getsum2)
First reason :
length(pairvector) = 1000
length(vvector.array) = 2048
So we are 5 times slower than the Vector{Tuple{Id, Float64}}
So it's slower because we use Vector{Union{Tuple{Id, Float64}, Nothing} and we have to test if elem !== nothing at each iteration. But there may be room for improvement.
EDIT : time to iterate over Vector{Tuple{Id, Float64}} and Vector{Union{Nothing, Tuple{Id, Float64}} are almost the same in Julia 1.3.
from discussion with @rrsadykov
Code : auxtest.txt
PMA is a vector, iterating over its element is the same as doing :
I timed the loop over a
Vector{Tuple{Id, Float64}}, PMA, andVector{Union{Nothing, Tuple{Id, Float64}}and indeed, the loop over the
Vector{Tuple{Int, Float64}}is the fastest.EDIT : test on julia1.4
First reason :
So we are 5 times slower than the
Vector{Tuple{Id, Float64}}So it's slower because we useVector{Union{Tuple{Id, Float64}, Nothing}and we have to test ifelem !== nothingat each iteration. But there may be room for improvement.EDIT : time to iterate over
Vector{Tuple{Id, Float64}}andVector{Union{Nothing, Tuple{Id, Float64}}are almost the same in Julia 1.3.from discussion with @rrsadykov
Code : auxtest.txt