While preparing an answer to Secret Santa - Generating 'valid' permutations I came across the need to check if a list contains zero. I am wondering what the fastest way to do this in Mathematica 7 is, with emphasis on a short list of non-negative integers.
Using Min[list] === 0
is the fastest I have found, faster than MemberQ
or FreeQ
, but I am hoping there is a faster way, on par with the BitXor
operation below:
r = Range@9;
p = Permutations@r;
BitXor[r, #] & /@ p; // Timing
0 === Min[BitXor[r, #]] & /@ p; // Timing
{0.062, Null}
{0.452, Null}
It appears that part of the slowdown with 0 === Min
as used in the question is interference with the autocompile of Map
. Separating these steps provides nearly a twofold improvement:
r = Range@9;
p = Permutations@r;
Min@# === 0 & /@ (BitXor[r, #] & /@ p); // Timing
{0.296, Null}
If I accept a result as (0|1) and if I process the list as one then I can use this:
Unitize[Times @@ BitXor[r, #] & /@ p]; // Timing
{0.156, Null}