Fast list-product sign for PackedArray?

As a continuation of my previous question, Simon's method to find the list product of a PackedArray is fast, but it does not work with negative values.

This can be "fixed" by Abs with minimal time penalty, but the sign is lost, so I will need to find the product sign separately.

The fastest method that I tried is EvenQ @ Total @ UnitStep[-lst]

lst = RandomReal[{-2, 2}, 5000000]; Do[ EvenQ@Total@UnitStep[-lst], {30} ] // Timing Out[]= {3.062, Null}

Is there a faster way?


This is a little over two times faster than your solution and apart from the nonsense of using Rule@@@ to extract the relevant term, I find it more clear - it simply counts the number elements with each sign.

EvenQ[-1 /. Rule@@@Tally@Sign[lst]]

To compare timings (and outputs)

In[1]:= lst=RandomReal[{-2,2},5000000]; s=t={}; Do[AppendTo[s,EvenQ@Total@UnitStep[-lst]],{10}];//Timing Do[AppendTo[t,EvenQ[-1/.Rule@@@Tally@Sign[lst]]],{10}];//Timing s==t Out[3]= {2.11,Null} Out[4]= {0.96,Null} Out[5]= True


A bit late-to-the-party post: if you are ultimately interested in speed, Compile with the C compilation target seems to be about twice faster than the fastest solution posted so far (Tally - Sign based):

fn = Compile[{{l, _Real, 1}}, Module[{sumneg = 0}, Do[If[i < 0, sumneg++], {i, l}]; EvenQ[sumneg]], CompilationTarget -> "C", RuntimeOptions -> "Speed"];

Here are the timings on my machine:

In[85]:= lst = RandomReal[{-2, 2}, 5000000]; s = t = q = {}; Do[AppendTo[s, EvenQ@Total@UnitStep[-lst]], {10}]; // Timing Do[AppendTo[t, EvenQ[-1 /. Rule @@@ Tally@Sign[lst]]], {10}]; // Timing Do[AppendTo[q, fn [lst]], {10}]; // Timing s == t == q Out[87]= {0.813, Null} Out[88]= {0.515, Null} Out[89]= {0.266, Null} Out[90]= True


  • How to reorganize a dataframe in R
  • Sorting/grouping odd and even numbers in odd and even segregated list
  • python: how to sort lists alphabetically with respect to capitalized letters
  • error in writing a text file in python [closed]
  • __init__ argument mismatch between super and subclass
  • Repeating elements of a list n times
  • Python: Default list in function
  • How to return that last index of a list Python [duplicate]
  • Manipulating sub matrices in R
  • How to add list of lists in a list comprehension
  • How do you know the index of an element that in the sub list
  • Modify list and dictionary during iteration, why does it fail on dict?
  • Python: how to split a list into an unknown number of smaller lists based on a delimeter
  • Missing parameter type
  • Search through a 2-dimensional list without numpy
  • Selecting dynamically created listboxs items in C#
  • CLISP : Check if two elements are in order one after another in a list
  • Fast list-product sign for PackedArray?
  • Python:How can i get all the elements in a list before the longest element?
  • Computing mean of all tuple values where 1st number is similar
  • What is expected when I am told “Sort a singly linked list”
  • multiple backgroundworker queueing
  • Fast hiding of intersecting rectangles
  • SQL Server 2008 INSERT Optimization
  • Fastest array lookup algorithm in C for embedded system?
  • stack every n columns of a matrix without apply in R
  • Returning semi-unique values from a list
  • SQL: Getting the physical size of a subset of a table
  • pip in virtualenv gets ConnectTimeoutError
  • perl, mysql - fasting way to upload a csv file into mysql?
  • How can I speed up CURL tasks?
  • Marklogic : Query response time is very high
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • TFS: Get latest causes slow project reloading
  • ActionScript 2 vs ActionScript 3 performance
  • Linker errors when using intrinsic function via function pointer
  • File upload with ng-file-upload throwing error
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • LevelDB C iterator
  • How can i traverse a binary tree from right to left in java?