BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:
I was running a piece of code that ran far slower than I expected and thrashed my cpu in the process. It turned out that I was accessing substrings of a substring via a substr reference, as in the 3rd loop below. On my system, under 5.8.6 and 5.10.0 this proves to be 2000 times slower than doing the same thing directly on the string or via a normal reference to that same string:
#! perl -slw use strict; use Time::HiRes qw[ time ]; my $string = 'X' x 1e6; my $ref = \$string; my $subRef = \substr $string, 0; my $start; my $x; $start = time; $x = substr $string, $_*50, 50 for 0 .. 2e4; print time() - $start; $start = time; $x = substr $$ref, $_*50, 50 for 0 .. 2e4; print time() - $start; $start = time; $x = substr $$subRef, $_*50, 50 for 0 .. 2e4; print time() - $start; __END__ c:\test>junk1 0.0107600688934326 0.0100328922271729 22.2601997852325 c:\test>\Perl510\bin\perl5.10.0.exe junk1.pl 0.00995421409606934 0.0109601020812988 21.9631989002228
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Access via substr refs 2000 times slower
by ikegami (Patriarch) on Dec 28, 2008 at 11:11 UTC | |
by BrowserUk (Patriarch) on Dec 28, 2008 at 11:53 UTC | |
by ikegami (Patriarch) on Dec 28, 2008 at 13:15 UTC | |
by tilly (Archbishop) on Dec 28, 2008 at 18:58 UTC | |
by BrowserUk (Patriarch) on Dec 28, 2008 at 21:39 UTC | |
by ikegami (Patriarch) on Dec 29, 2008 at 00:29 UTC | |
|
Re: Access via substr refs 2000 times slower
by talexb (Chancellor) on Dec 29, 2008 at 02:41 UTC | |
by BrowserUk (Patriarch) on Dec 29, 2008 at 13:53 UTC |