Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Determining uniqueness in a string.

by rtst (Initiate)
on Oct 02, 2005 at 04:38 UTC ( [id://496721]=note: print w/replies, xml ) Need Help??


in reply to Determining uniqueness in a string.

Sometimes verbose code is faster. This beat out the above regexp examples in all the cases I threw at it. (NOTE: the "Note unique" example is a best case scenario for this example, but the worst case of "All unique" still performs better").
use Benchmark qw( cmpthese ) ; my $digits = 2491306578; cmpthese( -5, { u1 => sub{ for ( 0 .. 9 ) { return 0 if $digits !~ /$_/;} return 1; }, u2 => sub{ my @x = sort split //, $digits; my $x = join "", @x; $x =~ tr/0-9//s; length $x == 10 ? return 1 : return 0; }, u3 => sub { return 0 if $digits =~ /(\d).*\1/; return 1; }, u4 => sub { return $digits !~ /(.).*\1/; }, u9 => sub { return 0 if index($digits, substr($digits,0,1), 1) != -1; return 0 if index($digits, substr($digits,1,1), 2) != -1; return 0 if index($digits, substr($digits,2,1), 3) != -1; return 0 if index($digits, substr($digits,3,1), 4) != -1; return 0 if index($digits, substr($digits,4,1), 5) != -1; return 0 if index($digits, substr($digits,5,1), 6) != -1; return 0 if index($digits, substr($digits,6,1), 7) != -1; return 0 if index($digits, substr($digits,7,1), 8) != -1; return 0 if index($digits, substr($digits,8,1), 9) != -1; return 1; }, } ); __END__
All unique (2491306578): Rate u1 u2 u3 u4 u9 u1 14387/s -- -71% -91% -91% -93% u2 50142/s 249% -- -67% -67% -74% u3 153182/s 965% 205% -- -0% -22% u4 153395/s 966% 206% 0% -- -22% u9 195811/s 1261% 291% 28% 28% -- Not unique (1102345678): Rate u1 u2 u3 u4 u9 u1 14381/s -- -71% -97% -97% -99% u2 50429/s 251% -- -91% -91% -97% u3 531431/s 3595% 954% -- -4% -65% u4 556425/s 3769% 1003% 5% -- -64% u9 1539566/s 10606% 2953% 190% 177% --

Replies are listed 'Best First'.
Re^2: Determining uniqueness in a string.
by thundergnat (Deacon) on Oct 02, 2005 at 14:29 UTC

    You have a point, though if I were going the verbose route, I would use the transliterate function rather than an index-substr. The back reference method come out slightly faster on my computer than index-substr with transliterate sightly ahead of both. It is moot however, tye's solution above (u5) pretty much spanks all of them.

    use Benchmark qw( cmpthese ) ; my $digits = 2491306578; cmpthese( -5, { u1 => sub{ for ( 0 .. 9 ) { return 0 if $digits !~ /$_/;} return 1; }, u2 => sub{ my @x = sort split //, $digits; my $x = join "", @x; $x =~ tr/0-9//s; length $x == 10 ? return 1 : return 0; }, u3 => sub { return 0 if $digits =~ /(\d).*\1/; return 1; }, u4 => sub { return $digits !~ /(.).*\1/; }, u5 => sub { return $digits =~ /^(?=.*?0)(?=.*?1)(?=.*?2)(?=.*?3)(?=.*?4) +(?=.*?5)(?=.*?6)(?=.*?7)(?=.*?8)(?=.*?9)/; }, u6 => sub { return 0 unless $digits =~ y/0/0/; return 0 unless $digits =~ y/1/1/; return 0 unless $digits =~ y/2/2/; return 0 unless $digits =~ y/3/3/; return 0 unless $digits =~ y/4/4/; return 0 unless $digits =~ y/5/5/; return 0 unless $digits =~ y/6/6/; return 0 unless $digits =~ y/7/7/; return 0 unless $digits =~ y/8/8/; return 0 unless $digits =~ y/9/9/; return 1; }, u9 => sub { return 0 if index($digits, substr($digits,0,1), 1) != -1; return 0 if index($digits, substr($digits,1,1), 2) != -1; return 0 if index($digits, substr($digits,2,1), 3) != -1; return 0 if index($digits, substr($digits,3,1), 4) != -1; return 0 if index($digits, substr($digits,4,1), 5) != -1; return 0 if index($digits, substr($digits,5,1), 6) != -1; return 0 if index($digits, substr($digits,6,1), 7) != -1; return 0 if index($digits, substr($digits,7,1), 8) != -1; return 0 if index($digits, substr($digits,8,1), 9) != -1; return 1; }, } ); __END__
    Rate u1 u2 u9 u4 u3 u6 u5 u1 10982/s -- -72% -95% -95% -95% -96% -97% u2 39808/s 262% -- -83% -83% -83% -85% -90% u9 229374/s 1989% 476% -- -4% -4% -12% -42% u4 238244/s 2069% 498% 4% -- -1% -9% -40% u3 240061/s 2086% 503% 5% 1% -- -8% -39% u6 261581/s 2282% 557% 14% 10% 9% -- -34% u5 394564/s 3493% 891% 72% 66% 64% 51% --
      You're measuring the speed of a positive match. How about the speed of determining failure? That's an important factor too.

      Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
      How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-03-29 13:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found