I just found out the time taken by a string assignment is not constant for a given argument. It's dependent on the previous state of the variable to which the string is assigned.
In your test, the time taken by $_ = '|0|0|0|0|0|0|'; is not constant because the previous state of $_ isn't constant. That means you aren't testing what you think you are testing. Using a lexical instead of $_ solves that problem.
Your tests really shouldn't be in subs either. They add a serious overhead, especially since your data is so small.
use strict; use warnings; use Benchmark qw( cmpthese ); my %tests = ( subst => '$x =~ s/.//;', substr_lval => 'substr($x,0,1) = "";', substr_mod => 'substr($x,0,1,"");', reverse => '$x = reverse $x; chop($x); $x = reverse($x);', substr_copy => '$x = substr($x,1);', ); for (values %tests) { $_ = 'use strict; use warnings; my $x = "|0|0|0|0|0|0|"; ' . $_; } cmpthese(-5, \%tests);
In reply to Re: Ways to delete start of string
by ikegami
in thread Ways to delete start of string
by hsmyers
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |