I think the only time performance of immutables can be better than mutables, is when they are also isbits. This is the case when all the fields of an immutable are isbits. Note that the compiler if free to either copy or reference immutables as the are immutable.
On Tue, 2016-01-26 at 09:19, Nitin Arora <[email protected]> wrote: > Okay thanks. > > But Is it not true that, if I pass the immutable Point type or an array of > the same to various functions, then Julia compiler will pass it by copying > instead of passing it by reference ? > > From the documentation: > *"An object with an immutable type is passed around (both in assignment > statements and in function calls) by copying, whereas a mutable type is > passed around by reference."* > > One the other hand a mutable types will be passed by reference. Doesn't > that suggest that an immutable type (singular or array) may actually be > worse in this scenario ? > > On Tuesday, January 26, 2016 at 12:03:19 AM UTC-8, Mauro wrote: >> >> > I assume from mutate you mean not changing type of the sub-variable (e.g >> > "vstate" above) ? I plan to change the values inside that vector but the >> > vector itself, both in length and type, will remain constant. >> >> Yes, then I'd use immutable. However, I don't think it will improve >> performance over mutable (as long as you don't replace the array may be >> worse). >> >> > Nitin >> > >> > On Monday, January 25, 2016 at 10:23:21 PM UTC-8, Yichao Yu wrote: >> >> >> >> On Tue, Jan 26, 2016 at 12:46 AM, Nitin Arora <[email protected] >> >> <javascript:>> wrote: >> >> > I have couple of questions ( maybe dumb :-) ) regarding composite >> types: >> >> > >> >> > 1) For a vector of composite type defined as: >> >> > >> >> > immutable Point{T<:AbstractFloat} >> >> > vstate :: Vector{T} # is a vector of length 6 which will be >> updated >> >> > during code execution >> >> > ct :: Vector{T} # is a vector of length 4 which will be >> updated >> >> > during code execution >> >> > id :: Int64 # is a constant input >> >> > end >> >> > N = 1000 >> >> > MyPoints = Array(Point,N) >> >> > >> >> > is it more useful / recommended to use immutable or mutable composite >> >> types >> >> > ? I am mainly concerned with memory allocation and performance while >> >> > accessing MyPoints. >> >> >> >> Use immutable if you don't need to mutate it. >> >> >> >> > >> >> > 2) If we have a immutable composite type as: >> >> > immutable Body{T} >> >> > μ :: T >> >> > end >> >> > >> >> > immutable PRB{T} #constant, doesn't change once set >> >> > bodlist :: Vector{T} #vector of another immutable type >> >> > end >> >> > b = Body(1.0) >> >> > prb = PRB([b]) >> >> > >> >> > and we access the variables as follows, results in memory allocation: >> >> > >> >> > @time b.μ >> >> > >> >> > 0.000002 seconds (5 allocations: 176 bytes) >> >> > >> >> > @time prb.bodlist[1].μ >> >> > >> >> > 0.000004 seconds (6 allocations: 192 bytes) >> >> > >> >> > >> >> > why is there a memory allocation every-time I access these variables >> ? >> >> (both >> >> > for nested and the un-nested case) >> >> >> >> And don't use @time in global scope like this. >> >> >> >> > >> >> > thanks for all the help, >> >> > Nitin >> >> >>
