I agree Inline::C will be fastest, but lookup tables can help a pure-Perl implementation a lot.

LUHN.pm:

package LUHN; use warnings; use strict; use List::Util qw(sum); use Data::Dump qw(dd); sub cd_kschwab { # pm#1226545 use integer; my $total = 0; my $flip = 1; foreach my $c (reverse split //, shift) { $c *= 2 unless $flip = !$flip; while ($c) { $total += $c % 10; $c = $c / 10; } } return (10 - $total % 10) % 10; } my @x2_cast_out_9 = ( # 0 1 2 3 4 5 6 7 8 9 # input number n 0, 2, 4, 6, 8, 1, 3, 5, 7, 9 # output: n * 2, cast out 9 ); my @check_digit = map { (10 - $_ % 10) % 10 } 0 .. 9 * 15; sub cd_AnomalousMonk { my $ccn = shift; my $total = 0; for (1, 3, 5, 7, 9, 11, 13) { $total += substr($ccn, $_, 1); } for (0, 2, 4, 6, 8, 10, 12, 14) { $total += $x2_cast_out_9[ substr($ccn, $_, 1) ]; } return $check_digit[ $total ]; } sub cd_BrowserUk { # pm#1226582 use integer; my $s = $_[ 0 ]; my $total = 0; for my $i ( 0 .. 14 ) { my $d = substr( $s, $i, 1 ); unless( $i & 1 ) { $d *= 2; $d -= 9 if $d > 9; } $total += $d; } $total *= 9; return chop $total; } 1;

test_LUHN_1.pl:

# test_LUHN_1.pl 01dec18waw use warnings; use strict; use Benchmark qw(cmpthese); use LUHN; for ('000' .. '999') { my $ccn = '401135000000' . $_; my $kschwab_cd = LUHN::cd_kschwab ($ccn); my $AnomalousMonk_cd = LUHN::cd_AnomalousMonk ($ccn); die "$ccn: kschwab $kschwab_cd != $AnomalousMonk_cd" if $kschwab_cd != $AnomalousMonk_cd; } print "tested cc number range kschwab vs. AnomalousMonk ok \n"; for ('000' .. '999') { my $ccn = '401135000000' . $_; my $kschwab_cd = LUHN::cd_kschwab ($ccn); my $BrowserUk_cd = LUHN::cd_BrowserUk ($ccn); die "$ccn: kschwab $kschwab_cd != $BrowserUk_cd" if $kschwab_cd != $BrowserUk_cd; } print "tested cc number range kschwab vs. BrowserUk ok \n"; cmpthese (-1, { cd_kschwab => sub { LUHN::cd_kschwab ('401135000000000') }, cd_AM => sub { LUHN::cd_AnomalousMonk ('401135000000000') }, cd_BrowserUk => sub { LUHN::cd_BrowserUk ('401135000000000') }, });

Output:

c:\@Work\Perl\monks\kschwab>perl test_LUHN_1.pl tested cc number range kschwab vs. AnomalousMonk ok tested cc number range kschwab vs. BrowserUk ok Rate cd_kschwab cd_BrowserUk cd_AM cd_kschwab 52764/s -- -40% -62% cd_BrowserUk 88033/s 67% -- -37% cd_AM 139999/s 165% 59% --

Update: My solution and some of the others are hard-wired for generating a checksum for a 15-digit string. Here's a generalized variation. Caution: this is not well tested. Also, I don't know that I like the  @check_digit look-up array all that much; it might chew up a lot of space in certain circumstances. BrowserUk's  $total *= 9;  return chop $total; eats less space and is only very slightly slower than the lookup.

sub cd_AM_HOP { # iterator solution my ($n_digits, # n of digits for which to generate luhn checksum ) = @_; my $order = $n_digits & 1; my @straight = grep +($_ & $order), 0 .. $n_digits-1; # dd '---', + \@straight; my @adjusted = grep !($_ & $order), 0 .. $n_digits-1; # dd '===', + \@adjusted; my @check_digit = map { (10 - $_ % 10) % 10 } 0 .. 9 * $n_digits; return sub { my $ccn = shift; # digits for which to generate checksum my $total = 0; for (@straight) { $total += substr($ccn, $_, 1); } for (@adjusted) { $total += $x2_cast_out_9[ substr($ccn, $_, 1) ]; } # return $check_digit[ $total ]; # return check digit $total *= 9; return chop $total; # return check digit }; # end sub iterator }


Give a man a fish:  <%-{-{-{-<


In reply to Re: Faster Luhn Check Digit Calculation? by AnomalousMonk
in thread Faster Luhn Check Digit Calculation? by kschwab

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.