Code improvement for DNA reverse complement?

crimaniak via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri May 19 15:53:39 PDT 2017


On Friday, 19 May 2017 at 12:55:05 UTC, Biotronic wrote:
> revComp6 seems to be the fastest, but it's probably also the 
> least readable (a common trade-off).
Try revComp7 with -release :)

string revComp7(string bps)
{
     char[] result = new char[bps.length];
     auto p1 = result.ptr;
     auto p2 = &bps[$ - 1];
     enum AT = 'A'^'T';
     enum CG = 'C'^'G';

     while (p2 > bps.ptr)
     {
        *p1 = *p2 ^ ((*p2 == 'A' || *p2 == 'T') ? AT : CG);
         p1++;
         p2--;
     }
     return result.assumeUnique;
}

In fact, when the size of the sequence is growing time difference 
between procedures is shrinking, so it's much more important to 
use memory-efficient presentation than to optimize logic.


More information about the Digitalmars-d-learn mailing list