use strict; # Purpose: In each benchmark invocation, have one use warnings; # (constant) string copied to another- which is then # modified (shortened by the first character) and for my $n (1..4) { # touched again (length determined and compared) use Benchmark qw(cmpthese); my $org_str = '|0' x 10**$n; # generate the string in local scope my $mod_str = $org_str; # do some allocation on the other string's PV print "string length: " . length($org_str) . "\n"; cmpthese( -3, { regexsubst => sub { # copy and modify ($mod_str = $org_str) =~ s/.//; die unless length($mod_str)+1 == length($org_str) }, substr_rhs => sub { # there's no point full string copy, simply copy what's needed $mod_str = substr($org_str, 1); die unless length($mod_str)+1 == length($org_str) }, substr_lhs => sub { # copy and modify substr($mod_str = $org_str, 0, 1) = ''; die unless length($mod_str)+1 == length($org_str) }, reversestr => sub { # reverse, copy, modify, reverse chop($mod_str = reverse($org_str)); $mod_str = reverse $mod_str; die unless length($mod_str)+1 == length($org_str) } } ); print '- ' x 30, "\n" }