Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
While significantly faster than the OP's

Actually, simply using index instead of m// in the grep makes my algorithm a bit faster than BrowserUk's regex. Granted, there's still a scalability problem here, but with the small tweak suggested by CombatSquirrel, it's much faster than the original, and works fine on data that is representative of what I'm actually using this for.

Here is the benchmark code I used:

#!/usr/bin/perl use warnings; use strict; use Benchmark qw(cmpthese timethese); my @test = ([ qw(fooabc123 fooabc321 foobca232) ], [ qw(abcfoo123 bcafoo321 foo123abc) ], [ qw(foo bor boz bzo) ]); for (@test) { die "regex,index" unless lcs_regex(@{$_}) eq lcs_index(@{$_}); die "index,buk" unless lcs_index(@{$_}) eq lcs_buk(@{$_}); } my $result = timethese(-5, { 'regex' => sub { lcs_regex(@{$_}) for @test }, 'index' => sub { lcs_index(@{$_}) for @test }, 'buk' => sub { lcs_buk(@{$_}) for @test }, }); cmpthese $result; sub lcs_regex { my $substr = $_[0]; my $len = length $_[0]; my $off = 0; while ($substr) { my @matches = grep /\Q$substr/, @_; #printf "%s%-".(length($_[0])-$off)."s matches %d\n", # " " x $off, $substr, scalar @matches; last if @matches == @_; $off++; $len-- and $off=0 if $off+$len > length $_[0]; $substr = substr $_[0], $off, $len; } return $substr; } sub lcs_index { my $substr = $_[0]; my $len = length $_[0]; my $off = 0; while ($substr) { my @matches = grep { -1 != index $_, $substr } @_; #printf "%s%-".(length($_[0])-$off)."s matches %d\n", # " " x $off, $substr, scalar @matches; last if @matches == @_; $off++; $len-- and $off=0 if $off+$len > length $_[0]; $substr = substr $_[0], $off, $len; } return $substr; } sub lcs_buk { my $strings = join "\0", @_; my $lcs; for my $n ( 1 .. length $strings ) { my $re = "(.{$n})" . '.*\0.*\1' x ( @_ - 1 ); last unless $strings =~ $re; $lcs = $1 } return $lcs; }

And here are the results I got:

Benchmark: running buk, index, regex for at least 5 CPU seconds... buk: 6 wallclock secs ( 5.31 usr + 0.01 sys = 5.32 CPU) @ 19 +73.50/s (n=10499) index: 5 wallclock secs ( 5.29 usr + 0.01 sys = 5.30 CPU) @ 30 +23.77/s (n=16026) regex: 6 wallclock secs ( 5.29 usr + 0.00 sys = 5.29 CPU) @ 93 +1.00/s (n=4925) Rate regex buk index regex 931/s -- -53% -69% buk 1973/s 112% -- -35% index 3024/s 225% 53% --

In reply to Re: Re: Re: finding longest common substring by revdiablo
in thread finding longest common substring by revdiablo

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 scrutinizing the Monastery: (5)
As of 2024-04-16 05:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found