octave-bug-tracker
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Octave-bug-tracker] [bug #63444] "if (COND)" is much slower than "if (a


From: John W. Eaton
Subject: [Octave-bug-tracker] [bug #63444] "if (COND)" is much slower than "if (all (COND))"
Date: Tue, 29 Nov 2022 12:50:14 -0500 (EST)

Follow-up Comment #1, bug #63444 (project octave):

The function tree_evaluator::visit_if_command_list in pt-eval.cc calls
tree_evaluator::is_logically_true, which calls octave_value::is_true.  If
you've already called "all(data)" and data is a vector, then it is fast
because you are looking at a scalar value.  For a double array, you end up in
octave_base_matrix<MT>::is_true in ov-base-mat.cc:


// Return true if this matrix has all true elements (non-zero, not NA/NaN).
template <typename MT>
bool
octave_base_matrix<MT>::is_true (void) const
{
  bool retval = false;
  dim_vector dv = m_matrix.dims ();
  int nel = dv.numel ();

  if (nel > 0)
    {
      MT t1 (m_matrix.reshape (dim_vector (nel, 1)));

      if (t1.any_element_is_nan ())
        octave::err_nan_to_logical_conversion ();

      if (nel > 1)
        warn_array_as_logical (dv);

      boolNDArray t2 = t1.all ();

      retval = t2(0);
    }

  return retval;
}


Reshape should be fast because it should just modify the dimensions, not copy
any data.  The slow part is that we are scanning for NaNs and then again for
zero values.  For speed, those could be combined in a single operation.  It
should probably be defined in a separate function the Array class rather than
doing the operation in octave_base_matrix<MT>::is_true.

This comparison also points out that "if (all (NaN))" is not the same as "if
(NaN)".  Is that difference compatible with Matlab?

We should rename this project from Octave to "pitfall".


    _______________________________________________________

Reply to this item at:

  <https://savannah.gnu.org/bugs/?63444>

_______________________________________________
Message sent via Savannah
https://savannah.gnu.org/




reply via email to

[Prev in Thread] Current Thread [Next in Thread]