title: Fastest way to count elements matching predicate
body: <p>Earlier today I asked if there's an <a href="https://stackoverflow.com/questions/10344339/what-is-the-idiomatic-way-of-counting-the-number-of-elements-matching-predicate">idiomatic way to count elements matching predicate</a> function in Mathematica, as I was concerned with performance.</p>
  
  <p>My initial approach for a given predicate <code>pred</code> was the following:</p>
  
  <pre><code>PredCount1[lst_, pred_] := Length@Select[lst, pred];
  </code></pre>
  
  <p>and I got a suggestion to instead use</p>
  
  <pre><code>PredCount2[lst_, pred_] := Count[lst, x_/;pred@x];
  </code></pre>
  
  <p>I started profiling these functions, with different <code>lst</code> sizes and <code>pred</code> functions, and added two more definitions:</p>
  
  <pre><code>PredCount3[lst_, pred_] := Count[Thread@pred@lst, True];
  PredCount4[lst_, pred_] := Total[If[pred@#, 1, 0] &amp; /@ lst];
  </code></pre>
  
  <p>My data samples were ranges between 1 and 10 million elements, and my test functions were <code>EvenQ</code>, <code>#&lt;5&amp;</code> and <code>PrimeQ</code>. The following graphs demonstrate time taken.</p>
  
  <p><strong>EvenQ</strong>
  <img src="https://i.stack.imgur.com/9YrSS.png" alt="EvenQ predicate"></p>
  
  <p>PredCount2 is slowest, 3 and 4 duke it out.</p>
  
  <p><strong>Comparison predicate: #&lt;5&amp;</strong></p>
  
  <p>I've selected this function, because it's close to what I need in my actual problem. Don't worry that this is a silly test function, it actually proves that the 4th function has some merit, which I actually ended up using it in my solution.</p>
  
  <p><img src="https://i.stack.imgur.com/E92Cb.png" alt="Less than five predicate"></p>
  
  <p>Same as <code>EvenQ</code>, but 3 is clearly slower than 4.</p>
  
  <p><strong>PrimeQ</strong>
  <img src="https://i.stack.imgur.com/Rlgx2.png" alt="PrimeQ predicate"></p>
  
  <p>This is just bizarre. Everything is flipped. I'm not suspecting caching as the culprit here, since worst values are for the function computed last.</p>
  
  <p>So, what's the right (fastest) way to count the number of elements in a list, that match a given predicate function?</p>