This seems to be a theme with V10, a new function dedicated to a specific task doesn't live up to expectation performance-wise. Mr. Wizard has already uncovered 2 such functions here and here. So how about AllTrue
versus VectorQ
? From the docs
We're interested in the second usage of VectorQ
here, which is the same as the purpose of AllTrue
. A quick test shows that VectorQ
annihilates AllTrue
when it comes to performance e.g.
Needs["GeneralUtilities`"]
AccurateTiming[AllTrue[Range[10^6], IntegerQ]]
0.20069455
While for VectorQ
:
AccurateTiming[VectorQ[Range[10^6], IntegerQ]]
0.00214923153
That is a two order of magnitude performance increase over AllTrue
. Using BenchMarkPlot
from the useful GeneralUtilities
package we observe the following:
BenchmarkPlot[
{VectorQ[#, IntegerQ] &, AllTrue[#, IntegerQ] &},
Range[#] &,
PowerRange[10, 10^8],
"IncludeFits" -> True, PlotRange -> Full
]
So, why is AllTrue
slow compared to VectorQ
?
VectorQ[..., IntegerQ]
has been specially overloaded on packed arrays to be constant time. In other words, the top-level evaluator doesn't actually evaluateIntegerQ
on every element in a list here. ButAllTrue
has no choice, being more general function, and has a linear complexity, as it should. $\endgroup$Range[10^6]
is slower thanVectorQ
. You might want to do the comparison with the range stored in a variable,data = Range[10^6]
. $\endgroup$Range
toVectorQ
. $\endgroup$