If you know the size in advance, it's always better to use that information. 
Use a comprehension, or allocate it at the final size and use a loop. sizehint 
is really for things like push!. Try timing different approaches, and you'll 
discover that sizehint helps some but does not make push! as fast as either of 
the other two options. But it's wonderful for those cases where you don't know 
the final size.

--Tim

On Sunday, May 18, 2014 01:06:02 PM Andrew Dabrowski wrote:
> Thanks.  What confused me is whether it's advantageous to prepare the new
> variable for the initial value, or whether that's done automatically.  I.e.
> is
> 
> newarray = f( oldarray )
> 
> OK or is it better to do
> 
> newarray = similar( oldarray )
> 
> followed by adding the elements of f( oldarray ) 1 by 1?  I gather from
> what you said that the latter is not necessary.
> 
> On Sunday, May 18, 2014 7:19:36 AM UTC-4, Tim Holy wrote:
> > On Saturday, May 17, 2014 02:04:49 PM Andrew Dabrowski wrote:
> > > But is it wrong to use it the way I suggested?
> > 
> > Not wrong, but whether it buys you anything depends on what comes next.
> > The
> > sizehint takes effect after the call to f(oldarray) returns, and it
> > doesn't
> > affect f at all. However, if the next thing you do with newarray is to
> > grow it,
> > then the sizehint might be useful.
> > 
> > Best,
> > --Tim
> > 
> > > On Saturday, May 17, 2014 4:27:19 PM UTC-4, Tim Holy wrote:
> > > > I use it right after creating an array that I'm going to be modifying
> > 
> > with
> > 
> > > > push!:
> > > > 
> > > > a = Array(Int, 0)
> > > > sizehint(a, 10^5)
> > > > 
> > > > Now I'll be able to push! at least 10^5 elements without it having to
> > 
> > re-
> > 
> > > > allocate and copy the data I've already added.
> > > > 
> > > > --Tim
> > > > 
> > > > On Saturday, May 17, 2014 09:29:49 AM Andrew Dabrowski wrote:
> > > > > What's the proper way to use sizehint when defining a new array?  Is
> > 
> > it
> > 
> > > > > newarray = sizehint( f( oldarray ), n ),
> > > > > 
> > > > > or do you have to already have the new variable defined before using
> > > > > sizehint?

Reply via email to