#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use experimental qw( signatures ); use String::Eertree; sub rosetta_old($str) { my @pal; for my $n (1 .. length $str) { for my $m (1 .. length $str) { my $strrev = ""; my $strpal = substr $str, $n - 1, $m; if ($strpal ne "") { for my $p (reverse 1 .. length $strpal) { $strrev .= substr $strpal, $p - 1, 1; } ($strpal eq $strrev) and push @pal, $strpal; } } } my %seen; return grep ! $seen{$_}++, @pal } sub rosetta($str) { my (@pal, %seen); for my $n (0 .. length($str) - 1) { for my $m (1 .. length $str) { my $strpal = substr $str, $n, $m; push @pal, $strpal if $strpal eq reverse($strpal) && ! $seen{$strpal}++; } } return @pal } my $s = join "", map chr(ord('a') + int rand 26), 1 .. 55 * 2; say $s; my @p1 = 'String::Eertree'->new(string => "$s")->uniq_palindromes; my @p2 = rosetta_old($s); my @p3 = rosetta($s); use Test2::V0 -no_srand => 1; say "Eertree: @p1"; say "Rosetta old: @p2"; say "Rosetta new: @p3"; like \@p2, bag { item $_ for @p1; end() }; like \@p3, bag { item $_ for @p1; end() }; use Benchmark qw{ cmpthese }; cmpthese(-3, { eertree => sub { 'String::Eertree'->new(string => $s)->uniq_palindromes }, rosetta_old => sub { rosetta_old($s) }, rosetta_new => sub { rosetta($s) }, }); done_testing();