bugGNU Octave - Bugs: bug #67762, Operating on logical arrays should...

 
 

bug #67762: Operating on logical arrays should be faster

Submitter:  Dmitri A. Sergatskov <dasergatskov>
Submitted:  Tue 02 Dec 2025 02:38:12 PM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Performance
Status:  Fixed Assigned to:  None
Originator Name:  Open/Closed:  * Closed
Release:  * dev Operating System:  * Any
Fixed Release:  11.1.0 (current default) Planned Release:  11.1.0 (current default)
* Mandatory Fields

Post a Comment

Add a New Comment Rich Markup
   

Discussion

Jump to the original submission

Wed 03 Dec 2025 03:27:51 PM UTC, comment #6: 

Thanks! I made some changes to prod, but then I realized that it is pretty much the same code, tried to revert, but I guess, left some junk behind.

Dmitri.
--

Dmitri A. Sergatskov <dasergatskov>
Wed 03 Dec 2025 03:12:24 PM UTC, comment #5: 

I pushed cset https://hg.savann ... /rev/dbbd168e73ce.

There was some code modifying prod, but it wasn't clear if that was complete.  There were no BIST tests for the change, as an example, nor any description in the ChangeLog message.  It also didn't make any difference to performance so I reverted that bit.

I added a note to NEWS.11 about the performance gains of up to 6X, impressive!

Marking as Fixed and closing report.


Rik <rik5>
Group administrator
Wed 03 Dec 2025 10:03:37 AM UTC, comment #4: 

"make check" passes, so push it!

Dmitri.
--

Dmitri A. Sergatskov <dasergatskov>
Wed 03 Dec 2025 09:59:08 AM UTC, comment #3: 

If it is this simple to improve performance on logical arrays, we should go ahead and do it.

Rik <rik5>
Group administrator
Wed 03 Dec 2025 09:46:50 AM UTC, comment #2: 

Attached (file bug67762_100.cset) optimizes bool input operation in sum, sumsq, and cumsum.
It avoid allocation/deallocation of full temporary double arrays (8x times the size ofthe input array) when operating on logical inputs. 
sum(bool), sumsq(bool), and cumsum(bool) now work directly on the underlying bool storage, allocating only the final double result.

Added some tests for bool operation.

Some benchmarks.
Before:

octave:1> a = randn(1e4);
octave:2> b = a > 0.5;
octave:3> tic; sum(b); toc ## bool array
Elapsed time is 0.52974 seconds.
octave:4> tic; meansq(a); toc ## "realistic" example
Elapsed time is 0.075659 seconds.
octave:5> tic; meansq(a, "omitnan"); toc
Elapsed time is 0.768257 seconds.

After:

octave:1> a = randn(1e4);
octave:2> b = a > 0.5;
octave:3> tic; sum(b); toc
Elapsed time is 0.0731111 seconds.
octave:4> tic; meansq(a); toc
Elapsed time is 0.0761449 seconds.
octave:5> tic; meansq(a, "omitnan"); toc
Elapsed time is 0.310125 seconds.


Dmitri.
--


(file #57906)

Dmitri A. Sergatskov <dasergatskov>
Tue 02 Dec 2025 04:49:03 PM UTC, comment #1: 

That's great, thanks!

Andreas Bertsatos <pr0m1th3as>
Group Member
Tue 02 Dec 2025 02:38:12 PM UTC, original submission:  

Currently sum(x) is slow if x is a logical array.

octave:1> x = rand(1e4);
octave:2> bx = x > 0.5;
octave:3> tic; sum(x); toc
Elapsed time is 0.074836 seconds.
octave:4> tic; sum(bx); toc
Elapsed time is 0.538059 seconds.


with a proper specialization (in data.cc):


@@ -4207,7 +4222,11 @@
       else if (isnative)
         retval = arg.bool_array_value ().any (dim);
       else
-        retval = arg.array_value ().sum (dim);
+        {
+          // OPTIMIZED: Fast boolean sum without conversion to double array
+          boolNDArray m = arg.bool_array_value ();
+          retval = do_mx_red_op<double, bool> (m, dim, mx_inline_count);
+        }
       break;

     default:


I see the timings become about the same (a little faster for logicals)


octave:2> x = randn(1e4);
octave:3> bx = x > 0.5;
octave:4> tic; sum (x); toc
Elapsed time is 0.075001 seconds.
octave:5> tic; sum (bx); toc
Elapsed time is 0.0733321 seconds.
octave:6>


I guess a similar change should be done to other functions.
("sumsq", "cumsum", may be "prod", something else?)

Dmitri.
--

Dmitri A. Sergatskov <dasergatskov>

 

Attached Files

Attached Files
file #57906:  bug67762_100.cset added by dasergatskov (6.5KiB - application/octet-stream)

(Note: upload size limit is set to 4.0MiB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

 

Dependencies

This item does not depend on any other items.

No items depend on this one.

 

Mail Notification Carbon-Copy List

Carbon-Copy List
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by pr0m1th3as (Posted a comment)
  • -email is unavailable- added by dasergatskov (Submitted the item)
  •  

    Votes

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Please enter the title of George Orwell's famous dystopian book (it's a date):

    History

    Follow 6 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2025-12-03 rik5 Fixed ReleaseNone 11.1.0 (current default)
    2025-12-03 rik5 Open/ClosedOpen Closed
    2025-12-03 rik5 StatusPatch Reviewed Fixed
    2025-12-03 rik5 StatusNone Patch Reviewed
        Planned ReleaseNone 11.1.0 (current default)
    2025-12-03 dasergatskov Attached File- Added bug67762_100.cset, #57906

    Back to the top

    Powered by Savane 3.16-a7ba.
    Corresponding source code