in reply to Re: Faster Luhn Check Digit Calculation?
in thread Faster Luhn Check Digit Calculation?
Benchmark: timing 10 iterations of Algorithm::LUHN, Tweaked Algorithm::LUHN, bdlightner... Algorithm::LUHN: 6 wallclock secs ( 6.63 usr + 0.00 sys = 6.63 CPU) @ 1.51/s (n=10) Tweaked Algorithm::LUHN: 5 wallclock secs ( 5.25 usr + 0.00 sys = 5.25 CPU) @ 1.90/s (n=10) bdlightner: 8 wallclock secs ( 7.81 usr + 0.00 sys = 7.81 CPU) @ 1.28/s (n=10)
# tweaked Algorithm::LUHN with "use integer" and some simplification sub cd3 { 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; }
|
|---|