My actual example is conceptually like this:
A Family has an Array of Children.
Families send their Children to Schools, so each School will have an Array of Children.
I do not want to copy the Children, so the School has an Array of Ref{Child}. Seems to work.
But accessing the Children via these Ref values seems glacial. I must be doing something wrong!!?? Here is a code example
type Foo
i::Int64
end
function as_array(foos::Array{Foo,1})
for f in foos
f.i += 1
end
end
function with_refs(foos::Array{Ref{Foo},1})
for f in foos
f.x.i += 1 # is this the right way to dereference a Ref? Seems to work...
end
end
foos = Array{Foo,1}() # couple of empty arrays
ref_foos = Array{Ref{Foo},1}()
for i in 1:100000 # fill the arrays
push!(foos, Foo(0))
push!(ref_foos, Ref(foos[end]))
end
as_array(foos); @time as_array(foos)
with_refs(ref_foos); @time with_refs(ref_foos)
for i in 1:5
@printf(STDERR, "i = %d foo.i %d %d\n", i, foos[i].i, ref_foos[i].x.i) # seems to be working
end
When I run this, I get
0.000405 seconds (130 allocations: 7.719 KB)
0.013922 seconds (4 allocations: 160 bytes)
of course my actual task is much longer running, but doing things via a reference is > 30x slower.
Was not able to find help anything like this. In the C++ world, having pointers to objects is quite natural, but how should one go about this in Julia?
Any advice most welcome…
thanks
Ian