Greetings!
I am reading Julius Krumbriegel’s (is he here?) informative article about the difference between tuples and vectors: jkrumbiegel.com - Tuples and Vectors, Allocations and Performance for Beginners
Example from the article:
rand_vector_point() = [rand(), rand()] # note the []
rand_tuple_point() = (rand(), rand()) # note the ()
# create vectors of 500 random points each
vector_points = [rand_vector_point() for _ in 1:500]
tuple_points = [rand_tuple_point() for _ in 1:500]
# define a simple function calculating pairwise differences
function difference_matrix(points)
[p1 .- p2 for p1 in points, p2 in points]
end
# run each version once, just to get compilation out of the way
difference_matrix(vector_points)
difference_matrix(tuple_points)
println("Vector version:")
@time difference_matrix(vector_points)
println("Tuple version:")
@time difference_matrix(tuple_points)
Output:
Vector version:
0.017412 seconds (250.00 k allocations: 24.796 MiB)
Tuple version:
0.002698 seconds (2 allocations: 3.815 MiB)
Having read the article, I am still unsure about some details.
-
The number of allocations that are shown: Are those the ones that happen during runtime? Because part of the memory should have been allocated in one go by the compiler upon compiling the function, which would then be the reason for the small number of allocations in the tuple case I assume.
-
The two allocations in the tuple version: Where do they come from and are they also there in the vector version? (they might have been lost in the rounding of
250.00 k
). -
What is the relationship of the actual allocated memory and the number of allocations? The allocated memory per allocation is much larger in the tuple case than in the vector case.