# Which second arguments is VectorQ/MatrixQ/ArrayQ optimized for?

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.

### VectorQ is specially optimized with the following functions

The following were tested in M11.3 unless stated otherwise. Past versions may behave differently.

• NumberQ, NumericQ (verified in M10.0)

• MachineNumberQ

• IntegerQ

• DeveloperMachineRealQ, DeveloperMachineIntegerQ, DeveloperMachineComplexQ (verified in M10.0 but see bug below)

• InternalPositiveIntegerQ, InternalNegativeIntegerQ, InternalNonNegativeIntegerQ, InternalNonPositiveIntegerQ (M11.1+ only)

• InternalPositiveMachineIntegerQ, InternalNegativeMachineIntegerQ, InternalNonNegativeMachineIntegerQ, InternalNonPositiveMachineIntegerQ (M11.1+ only)

• InternalRealValuedNumberQ, InternalRealValuedNumericQ

### Functions you might expect VectorQ to be optimized for, but it is not

Keep in mind that the following were tested in M11.3. Things may change in future versions.

• DeveloperRealQ
• Positive, Negative, NonPositive, NonNegative (these are not ...Q functions but optimizing them for packed arrays would make sense)

### Bugs to be aware of

In Mathematica 10.4 and earlier, the following return the incorrect False result:

VectorQ[{}, DeveloperMachineIntegerQ]
VectorQ[{}, DeveloperMachineRealQ]
VectorQ[{}, DeveloperMachineComplexQ]

• Is NumericQ as fast as InternalRealValuedNumericQ? – b3m2a1 Oct 23 '18 at 17:41
• @b3m2a1 I'm not sure, but thanks for pointing to that function! This post is not about the performance of these functions, but whether VectorQ recognizes them and avoids calling them entirely for better performance. – Szabolcs Oct 23 '18 at 18:40
• sorry that’s what I meant. I did the check and it also checks the packed-ness (it’s actually faster by some marginal fraction which might be its lack of generality). – b3m2a1 Oct 23 '18 at 20:52