http://www.perlmonks.org?node_id=1226543

kschwab has asked for the wisdom of the Perl Monks concerning the following question:

Trying to see if there's a faster way to generate the LUHN check digit that goes at the end of a credit card number. At the moment, I'm using this, cribbed from Algorithm::LUHN, only change was removing some not-needed input verification.
my %map = map { $_ => $_ } 0..9; sub check_digit { my @buf = reverse split //, shift; my $totalVal = 0; my $flip = 1; foreach my $c (@buf) { my $posVal = $map{$c}; $posVal *= 2 unless $flip = !$flip; while ($posVal) { $totalVal += $posVal % 10; $posVal = int($posVal / 10); } } return (10 - $totalVal % 10) % 10; } # example use for (401135000000000..401135000000099) { my $cd=check_digit($_); print "$_$cd\n"; }
You give it the first 15 digits of a credit card number, and it calculates the 16th digit. It's reasonably fast now, calculating about 135,000 cc numbers per second (older i7), but would like to see if there's anything that might speed it up, other than running more than one instance (already doing that, but trying to focus on just the single core speed here).