in reply to Ways to delete start of string
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);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Ways to delete start of string
by BrowserUk (Patriarch) on May 27, 2008 at 10:13 UTC | |
by ikegami (Patriarch) on May 27, 2008 at 19:23 UTC | |
by hsmyers (Canon) on May 27, 2008 at 17:12 UTC | |
by kyle (Abbot) on May 27, 2008 at 17:36 UTC | |
by tye (Sage) on May 27, 2008 at 18:00 UTC | |
by BrowserUk (Patriarch) on May 27, 2008 at 18:39 UTC | |
by hsmyers (Canon) on May 28, 2008 at 02:03 UTC | |
|
Re^2: Ways to delete start of string
by hsmyers (Canon) on May 27, 2008 at 06:47 UTC | |
by ikegami (Patriarch) on May 27, 2008 at 06:54 UTC |