in reply to Re^2: Faster Luhn Check Digit Calculation?
in thread Faster Luhn Check Digit Calculation?

Sorry. I didn't account for 0-based indices! This produces the correct result:

#! perl -slw use strict; my @samples = qw[ 4011350000000008 4011350000000016 4011350000000024 4011350000000032 4011350000000040 4011350000000057 4011350000000065 4011350000000073 4011350000000081 4011350000000099 ]; sub luhn { my $total = 0; for my $i ( 0 .. length( $_[0] ) -1 ) { my $d = substr( $_[0], $i, 1 ); unless( $i & 1 ) { $d *= 2; $d -=9 if $d > 9; } $total += $d; } $total *= 9; return substr $total, -1 } for ( @samples ) { print "$_ : ", luhn( substr $_, 0, 15 ); } __END__ C:\test>luhn 4011350000000008 : 8 4011350000000016 : 6 4011350000000024 : 4 4011350000000032 : 2 4011350000000040 : 0 4011350000000057 : 7 4011350000000065 : 5 4011350000000073 : 3 4011350000000081 : 1 4011350000000099 : 9

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit

Replies are listed 'Best First'.
Re^4: Faster Luhn Check Digit Calculation?
by BrowserUk (Patriarch) on Dec 01, 2018 at 02:54 UTC

    Some micro-ops yeild a further 40%:

    sub luhn { 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; }

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit
Re^4: Faster Luhn Check Digit Calculation?
by BrowserUk (Patriarch) on Dec 01, 2018 at 07:31 UTC

    And an Inline::C implementation of the algorithm is 20x faster still:

    #! perl -slw use strict; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'END_C', NAME => '_luhn', CLEAN_AFTER_BUILD =>0; int c_luhn( char *s ) { int i, total = 0; for( i=0; i < 15; ++i ) { int d = s[ i ] - '0'; if( !( i & 1 ) ) { d *= 2; if( d > 9 ) d -= 9; } total += d; } total *= 9; return total % 10; } END_C use Time::HiRes qw[ time ]; my @samples = qw[ 4011350000000008 4011350000000016 4011350000000024 4011350000000032 4011350000000040 4011350000000057 4011350000000065 4011350000000073 4011350000000081 4011350000000099 ]; sub luhn { 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; } for ( @samples ) { print "$_: ", luhn( substr $_, 0, 15 ); } my $start = time; for ( 401135000000000..401135000999999 ) { my $chk = luhn( $_ ); } printf "Took %.9f seconds.\n", time() - $start; for ( @samples ) { print "$_: ", c_luhn( $_ ); } my $start = time; for ( 401135000000000..401135000999999 ) { my $chk = c_luhn( $_ ); } printf "Took %.9f seconds.\n", time() - $start;

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit
      That's funny, I was converting yours to Inline::C at the same time.
      int cd2(char *number) { int i, d, total; total=0; for (i = 0; i<15; i++) { d=number[i]-48; if (!(i & 1)) { d *=2; if (d > 9) {d-=9;} } total+=d; } total *=9; return total % 10; }
      And, it is a little faster, appreciate it.
      $perl ./script
      Benchmark: timing 200 iterations of Inline::C (BrowserUK-His Conversion), Inline::C (BrowserUK-My Conversion), Inline::C (orig)...
      Inline::C (BrowserUK-His Conversion):  2 wallclock secs ( 2.54 usr +  0.00 sys =  2.54 CPU) @ 78.74/s (n=200)
      Inline::C (BrowserUK-My Conversion):  3 wallclock secs ( 2.51 usr +  0.00 sys =  2.51 CPU) @ 79.68/s (n=200)
      Inline::C (orig):  3 wallclock secs ( 2.97 usr +  0.00 sys =  2.97 CPU) @ 67.34/s (n=200)
      

        Unrolling the loop and using Anonymonk's lookup trick extracts another 5%:

        int l[] = { 0, 2, 4, 6, 8, 1, 3, 5, 7, 9 }; int c_luhn2( char *s ) { int total = 0; total += l[ s[ 0] - '0' ]; total += s[ 1] - '0'; total += l[ s[ 2] - '0' ]; total += s[ 3] - '0'; total += l[ s[ 4] - '0' ]; total += s[ 5] - '0'; total += l[ s[ 6] - '0' ]; total += s[ 7] - '0'; total += l[ s[ 8] - '0' ]; total += s[ 9] - '0'; total += l[ s[10] - '0' ]; total += s[11] - '0'; total += l[ s[12] - '0' ]; total += s[13] - '0'; total += l[ s[14] - '0' ]; total *= 9; return total % 10; }

        I doubt there is much fat left in there :)


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
        In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit