Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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% --

In reply to Re: Determining uniqueness in a string. by rtst
in thread Determining uniqueness in a string. by Yzzyx

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (9)
As of 2024-04-19 06:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found