Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I know Perl maintains a pointer (offset), within an SV, of where actual data in PV buffer starts, -- is this "beheading" the reason for the slowdown as demonstrated?
use strict; use warnings; use Benchmark 'cmpthese'; my $str = ' ' x 1e5; sub test1 { my $s = shift; my $copy = substr $str, 1; 1 while $copy =~ /./g; _: } sub test2 { my $s = shift; substr $s, 0, 1, ''; 1 while $s =~ /./g; _: } cmpthese -1, { test1 => sub { test1( $str )}, test2 => sub { test2( $str )}, }; # (warning: too few iterations for a reliable count) # Rate test2 test1 # test2 2.79/s -- -93% # test1 41.0/s 1372% --
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Are beheaded strings known to be slow?
by tonyc (Hermit) on Oct 10, 2025 at 00:24 UTC | |
by ikegami (Patriarch) on Oct 10, 2025 at 15:22 UTC | |
by Anonymous Monk on Oct 10, 2025 at 17:58 UTC | |
by NERDVANA (Priest) on Oct 11, 2025 at 14:24 UTC | |
by ikegami (Patriarch) on Oct 11, 2025 at 14:49 UTC | |
by NERDVANA (Priest) on Oct 12, 2025 at 19:39 UTC | |
|
Re: Are beheaded strings known to be slow?
by LanX (Saint) on Oct 09, 2025 at 18:05 UTC | |
by Anonymous Monk on Oct 09, 2025 at 18:13 UTC | |
|
Re: Are beheaded strings known to be slow?
by ikegami (Patriarch) on Oct 09, 2025 at 18:16 UTC |