use strict; use warnings; use Time::HiRes; my $startTime = [Time::HiRes::gettimeofday ()]; my $str = ''; $str .= int rand 10 for 1..60000; my @subs = sort map substr($str, $_, 400), 0..length($str)-1; # Find the longest common prefix between any two neighbors my $length = 0; my $value = ''; for my $i (0..$#subs-1) { my ($common) = map length, ($subs[$i] ^ $subs[$i+1]) =~ /^(\0*)/; if ($common > $length) { ($length, $value) = ($common, substr($subs[$i], 0, $common)); } } print "$value is the longest repeated substr\n"; print "Completed in " . Time::HiRes::tv_interval ($startTime) . "\n";