hrr has asked for the wisdom of the Perl Monks concerning the following question:
Now I have noted that my application considerably slows down on longer strings, and it seems that is due to the following effect, indicating that this approach scales as N^2 (instead of the naively expected N).my $n = unpack("v", $str); $str = substr($str, 1);
A similar issue was discussed in Access via substr refs 2000 times slower: I think the reason for this slowdown is that when constructing support for the lvalue property of substr, a copy is made. Is there a good workaround to this scaling issue? Instead of passing an ever-shrinking stream around different functions, I could just pass the whole string and an offset,$ time perl -e '$a = "a" x 20_000; $a = substr($a, 1) while length($a) +;' user 0m0.350s $ time perl -e '$a = "a" x 40_000; $a = substr($a, 1) while length($a) +;' user 0m0.791s $ time perl -e '$a = "a" x 80_000; $a = substr($a, 1) while length($a) +;' user 0m2.683s
but this seems rather clumsy to me... Are there more elegant ways of doing this?my $n = unpack("v", substr($str, $off, 2)); $off += 2;
my $n = unpack("v", substr($str, $off, 2, ""));
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Using substr to split a string scales as N^2 -- alternatives?
by ysth (Canon) on Jan 05, 2009 at 00:22 UTC | |
Re: Using substr to split a string scales as N^2 -- alternatives?
by ikegami (Patriarch) on Jan 05, 2009 at 00:25 UTC | |
by wol (Hermit) on Jan 05, 2009 at 14:23 UTC | |
by Tanktalus (Canon) on Jan 05, 2009 at 15:04 UTC | |
Re: Using substr to split a string scales as N^2 -- alternatives?
by BrowserUk (Patriarch) on Jan 05, 2009 at 00:34 UTC |