VectorQ
and similar functions (MatrixQ
, ArrayQ
) treat certain second arguments specially.
For example, this is very fast:
rr = RandomReal[{0.01, 1}, 100000];
VectorQ[rr, NumericQ]; // RepeatedTiming
(* {5.*10^-7, Null} *)
While this is not:
VectorQ[rr, Positive]; // RepeatedTiming
(* {0.022, Null} *)
This is because when rr
is a packed array, VectorQ
will not evaluate NumericQ
for each array element separately. Since all elements of a packed array are of the same type, it is clear what the answer should be without checking each of them.
Question: Which functions are treated specially by VectorQ
?
Why is this important? Constructs like VectorQ[#, NumericQ]&
are frequently used for argument checking. You'd want to know what patterns to use for optimal performance. Argument checking should ideally take negligible time compared to the actual computation the function does.