Tell me more ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question

1 Answer

up vote 18 down vote accepted

What you observed seems to be an instance of the general behavior of the pattern-matcher when used with what I call "syntactic patterns" - patterns which only reflect the rigid structure of an expression, like e.g. _f. The speed-up with respect to the scanning is because the main evaluation loop is avoided - for FreeQ and MemberQ, the scannng is done all inside the pattern-matcher, which is lower-level compared to the main evaluator.

In this answer, and also here, there are some examples of this behavior, and further discussion. I think that a good rule of thumb is that you gain an orderor an order and a half of magnitude speed-up by clever use of syntactic patterns in place of top-level scanning code (pushing all work into the pattern-matcher), and you gain 2-3 orders of magnitude speed-up if you manage to recast the problem as a vectorized numerical problem on packed arrays.

share|improve this answer
Would you add some examples to this, please? – Mr.Wizard Feb 6 '12 at 22:05
1  
@Spartacus I added links to two relevant questions / answers, containing some examples - will this satisfy you? I am afraid that otherwise right now I'd have to just copy those example and paste here, which doesn't make much sense IMO. – Leonid Shifrin Feb 6 '12 at 22:12
1  
@Spartacus Most functions that do their job "in one go" (Union, Split, etc.) seems to be fast too. – Szabolcs Feb 6 '12 at 22:13
2  
@Spartacus so now whenever we post a question we have to prove that we really don't know the answer and the question is nontrivial? Is this really what everybody wants? – acl Feb 7 '12 at 10:09
2  
@Spartacus I was not trying to blame you for that, merely expressing frustration with some attitudes here (not yours or Leonid's). I'll delete both this and the previous comment soon to avoid cluttering the place. – acl Feb 7 '12 at 12:41
show 5 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.