Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Yes, it is inspired by tye's version. However, in the spirit of TMTOWTDI:

sub Prefix { my ($m,$n) = (sort @_)[0,-1]; my $dif = $m ^ $n; my $len= length( ( $dif =~ /^(\0*)/ )[0] ); substr( $m, 0,$len ); }

Why sort? Since it's a builtin, it should be rather fast. Also, you're guaranteed that the first and last element of the sorted array will have the shortest prefix match of any two strings within the array. This sub is actually very fast.

use Benchmark; @files = qw(model4run1 model2run1 model4run2); sub antirice { my ($m,$n) = (sort @_)[0,-1]; my $dif= $m ^ $n; $len= length( ( $dif =~ /^(\0*)/ )[0] ); substr( $m, 0,$len ); } # not picking on Jenda, but he seems to have the fastest prior sub sub Jenda { my $first = shift(@_); my $and = $first; my $or = $first; foreach (@_) { $and &= $_; $or |= $_; } my $combined = $and ^ $or; $combined =~ s/[^\x00].*//; substr($first, 0, length($combined)); } print "Jenda: " . Jenda(@files) . "\n"; print "antirice: " . antirice(@files) . "\n"; timethese 100000, { Jenda => sub {Jenda(@files)}, antirice => sub {antirice(@files)} } __DATA__ Jenda: model antirice: model Benchmark: timing 100000 iterations of Jenda, antirice... Jenda: 1 wallclock secs ( 0.82 usr + 0.00 sys = 0.82 CPU) @ 12 +1904.76/s (n=100000) antirice: 1 wallclock secs ( 0.75 usr + 0.00 sys = 0.75 CPU) @ 13 +3333.33/s (n=100000)

The first rule of Perl club is - use Perl
ith rule of Perl club is - follow rule i - 1 for i > 1

In reply to Re: Find common prefix from a list of strings by antirice
in thread Find common prefix from a list of strings by Albannach

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (10)
    As of 2020-01-28 20:37 GMT
    Find Nodes?
      Voting Booth?