> 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 <nitin...@gmail.com
>> <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
>>

Reply via email to