[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: questions about recent changes
From: |
Jaroslav Hajek |
Subject: |
Re: questions about recent changes |
Date: |
Thu, 26 Mar 2009 19:40:45 +0100 |
On Thu, Mar 26, 2009 at 5:29 PM, John W. Eaton <address@hidden> wrote:
> You made the following change to Array.h:
>
> - dim_vector dims (void) const { return dimensions; }
> + // Return a const-reference so that dims ()(i) works efficiently.
> + const dim_vector& dims (void) const { return dimensions; }
>
> I'm not opposed to making a change like this, but we could make
> similar changes to many other functions in Octave, so maybe we should
> defer this until we can look at doing it more globally. It seems odd
> to me to just make the change in this one place. Also, I'm not sure I
> see the need to do this at all since dim_vectors are reference counted
> and copying them should be fairly fast (copy a pointer and increment a
> reference count). Also, how often is "dims()(i)" actually used? Does
> making this change have a noticeable effect on performance?
That's what I thought until recently, too; unfortunately, the reality
is much worse.
The reason is that dims () creates (until now) a non-const dim_vector
expression, and subsequently the non-const version of operator () was
called, which in turn forces a copy (!), because the object is aliased
(as you noted).
I think dims()(i) is not rare because there is no other method for
querying a single dimension; if there was, the change would not be
important.
I agree this should be changed in more places, at least in
octave_value and octave_base_value. I just didn't have enough time
today.
> You also changed dim_vecctor::numel so that it is now
>
> octave_idx_type numel (int n = 0) const
> {
> int n_dims = length ();
>
> octave_idx_type retval = 1;
>
> for (int i = n; i < n_dims; i++)
> retval *= elem (i);
>
> return retval;
> }
>
> With this definition and an object with dimensions [1, 0, 2, 3],
> dims.numel(2) will return 6. Is that what you want?
Yes.
> I suppose this
> could help with doing things like
>
> x = zeros (1, 0, 2, 3)
> x(1,:,:)
> => [](1,0,6)
>
> Is it used anywhere yet?
I just used it in Ffind, but I certainly needed it many times in the
past. See also dim_vector::redim.
> Should the Array::numel and octave_value::numel functions also accept
> an optional argument to specify the starting dimension? Maybe it
> would be helpful to add a comment explaining the intent of N here.
>
> jwe
>
Possibly. But since it can be done with octave_value::dims ().numel
(i), I don't see that as very important. I just wanted the operation
to be easily doable without writing a loop. I guess there will be some
equivalent loops scattered over the sources.
cheers
--
RNDr. Jaroslav Hajek
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz