Can this implementation of Damm algorithm be optimized?
Era Scarecrow via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Feb 9 10:34:30 PST 2017
On Thursday, 9 February 2017 at 17:36:11 UTC, Nestor wrote:
> I was trying to port C code from the article in Wikiversity [1]
> to D, but I'm not sure this implementation is the most
> efficient way to do it in D, so suggestions to optimize it are
> welcome:
>
> import std.stdio;
>
> static immutable char[] QG10Matrix =
> "03175986427092154863420687135917509834266123045978" ~
> "36742095815869720134894536201794386172052581436790";
>
> char checkDigit(string str) {
> char tmpdigit = '0';
> foreach(chr; str) tmpdigit = QG10Matrix[(chr - '0') +
> (tmpdigit - '0') * 10];
> return tmpdigit;
> }
Well one thing is you can probably reduce them from chars to just
bytes, instead of having to subtract you can instead add at the
end. Although unless you're working with a VERY large input you
won't see a difference.
Actually since you're also multiplying by 10, you can incorporate
that in the table too... (although a mixin might be better for
the conversion than by hand)
static immutable char[] QG10Matrix = [
00,30,10,70,50,90,80,60,40,20,
70,00,90,20,10,50,40,80,60,30,
40,20,00,60,80,70,10,30,50,90,
10,70,50,00,90,80,30,40,20,60,
60,10,20,30,00,40,50,90,70,80,
30,60,70,40,20,00,90,50,80,10,
50,80,60,90,70,20,00,10,30,40,
80,90,40,50,30,60,20,00,10,70,
90,40,30,80,60,10,70,20,00,50,
20,50,80,10,40,30,60,70,90,00];
char checkDigit(string str) {
char tmpdigit = 0;
foreach(chr; str) tmpdigit = QG10Matrix[(chr - '0') +
tmpdigit];
return (tmpdigit/10) + '0';
}
More information about the Digitalmars-d-learn
mailing list