title: String compression and comparison
body: <p>Recently, I started writing a program to compare DNA sequence.  As the alphabet only consists of four letters (ATCG), compressing each character to 2 bits seemed like it would offer faster comparisons (are two characters the same or different). However, when I ran a test char comparisons were much faster than bit comparisons (by ~30%).  Compression was carried out in both programs as a control.  What am I missing here?  Is there a more efficient way to compare bits?
  p.s. I also tried vector, but it was a bit slower than bitset.</p>
  
  <pre><code>// File: bittest.cc
  // Test use of bitset container
  
  #include &lt;ctime&gt;
  #include &lt;iostream&gt;
  #include &lt;bitset&gt;
  #include &lt;vector&gt;
  #include &lt;string&gt;
  using namespace std;
  
  void compress(string&amp;, bitset&lt;74&gt;&amp;);
  void compare(bitset&lt;74&gt;&amp;, bitset&lt;74&gt;&amp;);
  
  int main()
  {
     // Start timer
     std::clock_t start;
     double difference;
     start = std::clock();
  
     for(int i=0; i&lt;10000000; ++i){
        string frag1="ATCGACTGACTGACTGACTGACTGACTGACTGACTGA";
        string frag2="AACGAACGAACGAACGAACGAACGAACGAACGAACGA";
        int a=37;
        bitset&lt;74&gt; bits1;
        bitset&lt;74&gt; bits2;
        compress(frag1, bits1);
        compress(frag2, bits2);
        compare(bits1, bits2);
     }
  
     difference = ( std::clock() - start ) / (double)CLOCKS_PER_SEC;
     int minutes = difference/60;
     int seconds = difference - minutes * 60;
  
     if (seconds &lt; 10){
        cout &lt;&lt; "\nRunning time: " &lt;&lt; minutes &lt;&lt; ":0" &lt;&lt; seconds &lt;&lt; endl &lt;&lt; endl;
     }else{
        cout &lt;&lt; "\nRunning time: " &lt;&lt; minutes &lt;&lt; ":" &lt;&lt; seconds &lt;&lt; endl &lt;&lt; endl;
     }
  
     return 0;
  }
  
  void compress(string&amp; in, bitset&lt;74&gt;&amp; out){
     char c;
     int b=0;
     for(int i=0; i&lt;in.length(); ++i){
        c=in[i];
        b=2*i;
        switch(c){
          case 'A':
             break;
          case 'C':
             out.set(b+1);
             break;
          case 'G':
             out.set(b);
             break;
          case 'T':
             out.set(b);
             out.set(b+1);
             break;
          default:
             cout &lt;&lt; "Invalid character in fragment.\n";
        }
     }
  }
  
  void compare(bitset&lt;74&gt;&amp; a, bitset&lt;74&gt;&amp; b){
     for(int i=0; i&lt;74; ++i){
        if(a[i] != b[i]){
        }
     }
  }
  </code></pre>
  
  <p>And the string harness...</p>
  
  <pre><code>// File: bittest.cc
  
  #include &lt;ctime&gt;
  #include &lt;iostream&gt;
  #include &lt;bitset&gt;
  #include &lt;vector&gt;
  #include &lt;string&gt;
  using namespace std;
  
  void compress(string&amp;, bitset&lt;74&gt;&amp;);
  void compare(string&amp;, string&amp;);
  
  int main()
  {
     // Start timer
     std::clock_t start;
     double difference;
     start = std::clock();
  
     for(int i=0; i&lt;10000000; ++i){
        string frag1="ATCGACTGACTGACTGACTGACTGACTGACTGACTGA";
        string frag2="AACGAACGAACGAACGAACGAACGAACGAACGAACGA";
        int a=37;
        bitset&lt;74&gt; bits1;
        bitset&lt;74&gt; bits2;
        compress(frag1, bits1);
        compress(frag2, bits2);
        compare(frag1, frag2);
     }
  
     difference = ( std::clock() - start ) / (double)CLOCKS_PER_SEC;
     int minutes = difference/60;
     int seconds = difference - minutes * 60;
  
     if (seconds &lt; 10){
        cout &lt;&lt; "\nRunning time: " &lt;&lt; minutes &lt;&lt; ":0" &lt;&lt; seconds &lt;&lt; endl &lt;&lt; endl;
     }else{
        cout &lt;&lt; "\nRunning time: " &lt;&lt; minutes &lt;&lt; ":" &lt;&lt; seconds &lt;&lt; endl &lt;&lt; endl;
     }
  
     return 0;
  }
  
  void compress(string&amp; in, bitset&lt;74&gt;&amp; out){
     char c;
     int b=0;
     for(int i=0; i&lt;in.length(); ++i){
        c=in[i];
        b=2*i;
        switch(c){
          case 'A':
             break;
          case 'C':
             out.set(b+1);
             break;
          case 'G':
             out.set(b);
             break;
          case 'T':
             out.set(b);
             out.set(b+1);
             break;
          default:
             cout &lt;&lt; "Invalid character in frag.\n";
        }
     }
  }
  
  void compare(string&amp; a, string&amp; b){
     for(int i=0; i&lt;37; ++i){
        if(a[i] != b[i]){
        }
     }
  }
  </code></pre>