String compare performance

bearophile bearophileHUGS at lycos.com
Sat Nov 27 11:09:06 PST 2010


Ellery Newcomer:

> how well does dmd perform when you use a switch statement?

4th D version:

import std.file: read;
import std.c.stdio: printf;

int test(char[] data) {
    int count;
    foreach (i; 0 ..  data.length - 3) {
        switch (data[i .. i + 3]) {
            case "TAG", "TGA", "TAA":
                count++;
            default:
        }
    }
    return count;
}

void main() {
    char[] data0 = cast(char[])read("data.txt");
    int n = 300;
    char[] data = new char[data0.length * n];
    for (size_t pos; pos < data.length; pos += data0.length)
        data[pos .. pos+data0.length] = data0;

    printf("%d\n", test(data));
}



Timings, dmd compiler, best of 4, seconds:
  D #1: 5.72
  D #4: 1.84
  Psy:  1.59
  D #2: 0.55
  D #3: 0.34

Bye,
bearophile


More information about the Digitalmars-d mailing list