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

Re: Re: Re: finding longest common substring

by revdiablo (Prior)
on Nov 20, 2003 at 03:30 UTC ( [id://308482]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: finding longest common substring
in thread finding longest common substring

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% --

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (7)
As of 2024-03-28 21:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found