title: C++ What is the fastest way to scan for certain elements within a unsigned char array and a unsigned char vector?
body: <p>I have a small question, what is the FASTEST way to scan for certain elements within a LARGE unsigned char array and a vector that contains only unsigned char elements? Straight answer would be great, but in-depth detailed answer would be awesome. What do I mean by fast? Basically, to search for certain characters within at least a second. I know that wasn't a very educated definition...</p>
  
  <p>Note: The array is not sorted.</p>
  
  <p><strong>Common Declaration:</strong></p>
  
  <pre><code>unsigned char* Array = new unsigned char[ 50000 ];
  std::vector&lt; unsigned char &gt; Vec( 50000 );
  /*
   * Fill Array &amp; Vec with random bytes
   */
  </code></pre>
  
  <p>Lets say, I want to search for the letter 'a' in Array, I would simply write this loop to search for it:</p>
  
  <p>Note: The searching process will be search for more than one elements. Mainly, 256. Hence, you can exploit that magic number.</p>
  
  <p><strong>For loop method:</strong></p>
  
  <pre><code>unsigned int Count = 0;
  for ( unsigned int Index = 0; Index != 50000; ++ Index )
     if( Array[ Index ] == 'a' ) Count ++;
  </code></pre>
  
  <p><strong>std::count method:</strong></p>
  
  <pre><code>unsigned int Count = std::count ( Array, Array + 50000, 'a' );
  </code></pre>
  
  <p>Are there any faster way to search for certain elements within Array?</p>
  
  <p>Some IDEAS - Please don't give me a thumbs down for this! Its only an idea. I want some opinions.</p>
  
  <p><strong>Sorting</strong></p>
  
  <p>Would the speed be better if we made a copy of Array and sort it? Why make a copy? Well, because we need to keep the original content. The goal is to basically scan and count the occurrence of a character. Remember, speed matter. That means, the copying process must be fast.</p>
  
  <pre><code>Answer: No and its not worth it!
  </code></pre>
  
  <p>Why? Well, lets read this:</p>
  
  <p>@Kiril Kirov:</p>
  
  <blockquote>
    <p>Depends. If you plan to search for a single char -
    absolutely not. Copying the array is an expensive operation. Sorting it - even more expensive.</p>
    
    <p>Well, if you will have only one array and you plan to search for, let's say, 100 different characters, then this method could give you a better performance. Now, this really depends on your usage. And nobody will be able to give you the absolutely correct answer for this case. You need to run it and profile.</p>
  </blockquote>
  
  <p>*Scroll down to @Kiril Krov's informative post for more.</p>
  
  <p><strong>Answer:</strong>
  So far, there isn't a solid or an answer, because there isn't a really "fast" method to achieve this goal, especially when its not SORTED. However, <em>threads</em> could be a possible solution. But, watch out for our CPU! This was based on @Andrea's submitted answer (scroll down a little more for more info) - I hoped I read it right.</p>