First of all thanks to all of those who answered questions in my previous
post.
I ran into another, rather confusing behavior:
Say we define an immutable type as:
immutable Point{T<:AbstractFloat}
state :: Vector{T} # State Vector (length = 6)
dstate :: Vector{T} # Derivative vector wrt. time (length = 6)
ct :: Vector{T} #control vector
id :: Int64 # is an input
cb :: T # is an input
end
Pts = Array(Point,10)
Now Let us define a function which can initialize the variable "Pts" above
( an array of Points ):
function fillpts!(PointVec::Vector{Point})
for j = 1:10
states = rand(6); dstates = rand(6); ctrl = rand(4); tar = 1; cb =
rand(1)[1]
PointVec[j] =
Point{Float64}(copy(states),copy(dstates),copy(ctrl),tar,cb)
end
return nothing
end
fillpts!(Pts)
Now Lets define two functions (one works on array of Point, other one a
single Point type) which check for memory allocations when accessing the
member variables:
function checkAllocation(P::Vector{Point})
@time for i = 1:10000
P[1].state[1]
end
@time for i= 1:10000
P[1].cb
end
@time for i =1:10000
P[1].id
end
return nothing
end
#------------------------------------------
function checkAllocation_Single(P::Point)
@time for i = 1:10000
P.state[1]
end
@time for i= 1:10000
P.cb
end
@time for i =1:10000
P.id
end
return nothing
end
Finally, the result of the benchmarking is as follows:
checkAllocation(Pts)
# Results
# 0.000459 seconds (10.00 k allocations: 156.250 KB)
# 0.000234 seconds (10.00 k allocations: 156.250 KB)
# 0.000126 seconds
checkAllocation_Single(Pts[1])
# Results
# 0.000003 seconds
# 0.000000 seconds
# 0.000000 seconds
*My Question:*
For the first function checkAllocation, which works on array on immutable
composite type, why do I get memory allocation when accessing an array or a
Float64 , but none when I access an integer (the sub variable "id") ? This
seems very strange to me. Whats so special about a Float ?
I was hoping and expecting to be no memory allocation for accessing these
values.
This behavior is more in line with what I expected with the second
function, where we see no memory allocation.
Can someone more knowledgable please explain whats going on here ? I am
really confused.
again, thanks for the help!
Nitin