sub findCycle { my $str = @_ ? shift : ''; $str =~ /^(.*?)\1*$/; return wantarray ? ( length $1, $1 ) : length $1; } #### Rate 0 1 2 0 906/s -- -58% -85% 1 2143/s 137% -- -64% 2 5966/s 559% 178% -- #### Rate 0 2 1 0 17.0/s -- -89% -99% 2 155/s 812% -- -94% 1 2465/s 14431% 1494% -- #### Rate 2 1 0 2 222/s -- -96% -98% 1 5023/s 2161% -- -52% 0 10479/s 4615% 109% -- #### use strict; use warnings; use Math::Pari 'divisors'; use Benchmark 'cmpthese'; my $len = shift || 3; srand 0; { $::input = join '', map int( rand 9 ), 1..$len; last if length $::input == findCycle_0( $::input ); redo; } $::input x= int( 10_000 / $len ); cmpthese( -1, { 0 => 'scalar findCycle_0( $::input )', 1 => 'scalar findCycle_1( $::input )', 2 => 'scalar findCycle_2( $::input )', } ); sub findCycle_0 { my $str = shift; my $copy = $str; my $strLen = length $str; for ( 1 .. $strLen/2 ) { $copy .= substr $copy, 0, 1, ''; return wantarray ? ($_, substr $str, 0, $_) : $_ if $str eq $copy; } return wantarray ? ($strLen, $str) : $strLen; } sub findCycle_1 { my $str = shift; my $strLen = length $str; for ( @{ divisors( $strLen ) } ) { my $copy = $str; $copy .= substr( $copy, 0, $_, '' ); return wantarray ? ($_, substr $str, 0, $_) : $_ if $str eq $copy; } } sub findCycle_2 { my $str = @_ ? shift : ''; $str =~ /^(.*?)\1*$/; return wantarray ? ( length $1, $1 ) : length $1; }