String compare performance
bearophile
bearophileHUGS at lycos.com
Sat Nov 27 14:46:00 PST 2010
denis.spir
> What's your diagnostic on why basic string compare parforms the way it does?
I prefer the numbers and asm listings speak for themselves, because my diagnostic sometimes is wrong. But I guess it's caused by three not inlined calls to __adEq2 each loop, done to compare 3 char long arrays.
> And have you tried a naive hand-written strComp (just for the sake of completeness) like:
Here it is:
Timings, dmd compiler, best of 4, seconds:
D #1: 5.72
D #4: 1.84
D #5: 1.73
Psy: 1.59
D #2: 0.55
D #3: 0.34
// D version #5
import std.file: read;
import std.c.stdio: printf;
bool cmp2(char[] s1, string s2) {
if (s1.length != s2.length)
return false;
foreach (uint i ; 0 .. s1.length)
if (s1[i] != s2[i])
return false;
return true;
}
int test(char[] data) {
int count;
foreach (i; 0 .. data.length - 3) {
char[] codon = data[i .. i + 3];
if (cmp2(codon, "TAG") || cmp2(codon, "TGA") || cmp2(codon, "TAA"))
count++;
}
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));
}
> Also, is there a way to bit-compare given memory areas at much higher speed than element per element (I mean for arrays in general)?
I don't know. I think you can't.
Bye,
bearophile
More information about the Digitalmars-d
mailing list