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: Rik
Subject: [Octave-bug-tracker] [bug #63444] "if (COND)" is much slower than "if (all (COND))"
Date: Tue, 29 Nov 2022 14:53:15 -0500 (EST)

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

I don't think looping over the array twice is that big a deal because it is
what is inside the body of the loop which is taking the time rather than the
loop structure itself (at least in C++, not Octave).

I made a test case as a .oct file.  This is attached as loop2.cc and shown
below.


#include <octave/oct.h>

DEFUN_DLD (loop2, args, nargout,
           "Test single or double loop equivalent to all()\n"
           "ARG1 : TRUE/FALSE, do 1 combined loop?\n"
           "ARG2 : numeric array")
{
  if (args.length () != 2)
    print_usage ();

  bool do_1_loop = args(0).bool_value ();
  NDArray A = args(1).array_value ();
  octave_idx_type N = A.numel ();
  bool retval = true;

  if (do_1_loop)
    {
      for (octave_idx_type i = 0; i < N; i++)
        {
          if (std::isnan (A(i)))
            error ("loop2: Can not convert NaN to logical value");
          if (! A(i))
            retval = false;
        }
    }
  else
    {
      for (octave_idx_type i = 0; i < N; i++)
        {
          if (std::isnan (A(i)))
            error ("loop2: Can not convert NaN to logical value");
        }
      for (octave_idx_type i = 0; i < N; i++)
        {
          if (! A(i))
            retval = false;
        }
    }

  return ovl (retval);
}


I then ran the benchmark and it is basically equivalent.


octave:7> tic; for i = 1:100, loop2 (0,x); end; toc
Elapsed time is 0.385158 seconds.

octave:10> tic; for i = 1:100, loop2 (1,x); end; toc
Elapsed time is 0.382482 seconds.




(file #54039)

    _______________________________________________________

Additional Item Attachment:

File name: loop2.cc                       Size:0 KB
    <https://file.savannah.gnu.org/file/loop2.cc?file_id=54039>



    _______________________________________________________

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]