No one gives a summary of explicit timing. Here is one
First, define
ClearAll[timing]
timing[a_] := {
Prepend[And @@ a; // RepeatedTiming, "And@@a"],
Prepend[FreeQ[a, False]; // RepeatedTiming, "FreeQ[a,False]"],
Prepend[MemberQ[a, False]; // RepeatedTiming, "MemberQ[a,False]"],
Prepend[VectorQ[a, TrueQ]; // RepeatedTiming, "VectorQ[a,TrueQ]"],
Prepend[AllTrue[a, TrueQ]; // RepeatedTiming, "AllTrue[a,TrueQ]"],
Prepend[AnyTrue[a, TrueQ]; // RepeatedTiming,
"AnyTrue[a,TrueQ]"]} // TableForm
Even random True and False mixed List
a = RandomChoice[{True, False}, 10^6];
timing[a]
gives
7.05217*10^-6 Null And@@a
3.86491*10^-7 Null FreeQ[a,False]
3.76835*10^-7 Null MemberQ[a,False]
2.95758*10^-7 Null VectorQ[a,TrueQ]
2.88454*10^-7 Null AllTrue[a,TrueQ]
3.70405*10^-7 Null AnyTrue[a,TrueQ]
All True List
a = RandomChoice[{True}, 10^6];
timing[a]
gives
0.0139276 Null And@@a
3.06406*10^-7 Null FreeQ[a,False]
3.06094*10^-7 Null MemberQ[a,False]
0.0774998 Null VectorQ[a,TrueQ]
0.0791204 Null AllTrue[a,TrueQ]
2.84386*10^-7 Null AnyTrue[a,TrueQ]
All False List
a = RandomChoice[{False}, 10^6];
timing[a]
gives
0.0085237 Null And@@a
4.08738*10^-7 Null FreeQ[a,False]
4.20621*10^-7 Null MemberQ[a,False]
3.25424*10^-7 Null VectorQ[a,TrueQ]
3.15501*10^-7 Null AllTrue[a,TrueQ]
0.0902116 Null AnyTrue[a,TrueQ]
It seems FreeQ
and MemberQ
are best
But VectorQ
is good for packed array
s = ConstantArray[1, 1000000];
FreeQ[IntegerQ /@ s, True]; // RepeatedTiming
VectorQ[s, IntegerQ]; // RepeatedTiming
s = Developer`FromPackedArray@ConstantArray[1, 1000000];
FreeQ[IntegerQ /@ s, True]; // RepeatedTiming
VectorQ[s, IntegerQ]; // RepeatedTiming
gives
{0.111584, Null}
{1.91957*10^-7, Null}
{0.107412, Null}
{0.00319087, Null}
AllOf[b_List] := And @@ b
andAnyOf[b_List] := Or @@ b
... $\endgroup$MemberQ
andFreeQ
. $\endgroup$