I would like to understand the implementation that allows MemberQ and FreeQ to be as fast as they are.
I noticed this thanks to this fine answer.
I start with a list of True|False values:
lst = Insert[Table[True, {500000}], False, 499000];
It is not packed:
Developer`PackedArrayQ[lst]
False
I compare timings:
Scan[Identity, lst] ~Do~ {100} // Timing
MemberQ[lst, False] ~Do~ {100} // Timing
FreeQ[lst, False] ~Do~ {100} // Timing
{4.93, Null}
{0.405, Null}
{0.25, Null}
What allows these functions to be more than an order of magnitude faster than simply Scanning the list?
Union,Split, etc.) seems to be fast too. – Szabolcs Feb 06 '12 at 22:13Unionswitched to a pairwise comparisons for explicit comparison function, which leads to a quadratic-time algorithm, soUnion, as well asDeleteDuplicates, are bad examples here.SortandSplitare good examples). This is probably what you meant by "in one go" - they don't have to oscillate between the kernel code and high-level evaluations. It is these oscillations that kill the performance. – Leonid Shifrin Feb 06 '12 at 22:17Headspecification (as inx_Head) count as what you call "syntactic patterns"? I would have thought that checking for matched head is just like conditionals... (i.e. Doesxhave the rightHead?) – QuantumDot Nov 08 '14 at 18:07Alternativescompared withCondition? – QuantumDot Nov 08 '14 at 18:11_headis a good example ofsyntacticpattern. As toAlternatives, it is still syntactic pattern (as far as the alternative patterns are all syntactic), since it only involves pattern-matcher and not evaluator. – Leonid Shifrin Nov 08 '14 at 19:26