Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Faster Luhn Check Digit Calculation?

by BrowserUk (Patriarch)
on Nov 30, 2018 at 19:40 UTC ( [id://1226551]=note: print w/replies, xml ) Need Help??


in reply to Faster Luhn Check Digit Calculation?

I doubt it'll beat the C version, but I think it should beat the Perl versions and the algorithm (which I think is correct but haven't verified) would readily convert to C:

sub luhn { my $total = 0; for my $i ( 0 .. length $_[0] ) { my $d = substr $_[0], $i, 1 ); if( $i & 1 ) { $d *= 2; $s -=9 if $d > 10; } $total += $d; } $total *= 9; return substr $total, -1 }

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^2: Faster Luhn Check Digit Calculation?
by kschwab (Vicar) on Nov 30, 2018 at 20:41 UTC

    It is a little faster than my previous Perl attempts...

    Benchmark: timing 100 iterations of BrowserUK, Inline::C, Algorithm::LUHN...
     BrowserUK: 46 wallclock secs (46.03 usr +  0.00 sys = 46.03 CPU) @  2.17/s (n=100)
     Inline::C:  1 wallclock secs ( 1.28 usr +  0.00 sys =  1.28 CPU) @ 78.12/s (n=100)
     Algorithm::LUHN: 49 wallclock secs (48.58 usr +  0.00 sys = 48.58 CPU) @  2.06/s (n=100)
    

    But, it's not producing the right checksum digit. It should produce:

    4011350000000008
    4011350000000016
    4011350000000024
    4011350000000032
    4011350000000040
    4011350000000057
    4011350000000065
    4011350000000073
    4011350000000081
    4011350000000099
    ...
    
    But, it's producing:
    4011350000000000
    4011350000000019
    4011350000000028
    4011350000000037
    4011350000000046
    4011350000000055
    4011350000000064
    4011350000000073
    4011350000000082
    4011350000000091
    ...
    

      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

        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

        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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1226551]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-25 12:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found