renodino has asked for the wisdom of the Perl Monks concerning the following question:
I'm managing some large binary buffers (64K+, handled as scalars). I need to modify various pieces of the buffer in situ. Logically, using pack(), unpack(), and substr(), everything works fine.
But what of performance and passing the buffers around between methods/objects ? Since perl is pass-by-value, I assume my big buffer gets copied unless I explicitly pass it as a ref. But what happens with substr() (or other builtins) ? Are they optmized to use the underlying SV wo/ making a copy ? Obviously, lhs substr() modifies the original, but is it making a copy, modifying the copy, then replacing the SV's buffer, or does it truly work in situ ?
I've tried perusing the perl source, but there are so many substr's, I don't know which is which.
Update:
Many thanks to all the responders. Just to get a sense of the impact, I wrote a little (imprecise) test script:
and got these results (WinXP, 2.4GHz, AS 5.8.6):use Time::HiRes qw(time); my $buffer = "\0" x 65000; my $start = time(); substr($buffer, 10, 4) = pack('L', $_) foreach (1..900000); print "inline took ", (time() - $start), " secs\n"; $start = time(); bySVBuffer3Arg($buffer, $_) foreach (1..900000); print "bySVBuffer3Arg() took ", (time() - $start), " secs\n"; $start = time(); bySVBuffer4Arg($buffer, $_) foreach (1..900000); print "bySVBuffer4Arg() took ", (time() - $start), " secs\n"; $start = time(); byRefBuffer(\$buffer, \$_) foreach (1..900000); print "byRefBuffer() took ", (time() - $start), " secs\n"; $start = time(); byCopyBuffer($buffer, $_) foreach (1..900000); print "byCopyBuffer() took ", (time() - $start), " secs\n"; sub byCopyBuffer { my ($buf, $val) = @_; substr($buf, 10, 4) = pack('L', $val); return 1; } sub byRefBuffer { my ($buf, $val) = @_; substr($$buf, 10, 4, pack('L', $$val)); return 1; } sub bySVBuffer3Arg { substr($_[0], 10, 4) = pack('L', $_[1]); return 1; } sub bySVBuffer4Arg { substr($_[0], 10, 4, pack('L', $_[1])); return 1; }
C:\Perl>perl bufref.pl inline took 0.919242858886719 secs bySVBuffer3Arg() took 1.52185487747192 secs bySVBuffer4Arg() took 1.23141598701477 secs byRefBuffer() took 2.32297611236572 secs byCopyBuffer() took 20.3989539146423 secsSo even passing refs is about half the speed of direct param manipulation. And the copy is really expensive.
Update 2:
Updated above code to include a 4 arg substr() direct from params, which seems about 20% faster than an lhs 3 arg substr().
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: lhs substr(): refs vs. scalars
by BrowserUk (Patriarch) on Oct 08, 2005 at 17:06 UTC | |
by renodino (Curate) on Oct 08, 2005 at 18:24 UTC | |
by BrowserUk (Patriarch) on Oct 08, 2005 at 18:57 UTC | |
by renodino (Curate) on Oct 08, 2005 at 19:41 UTC | |
by ysth (Canon) on Oct 09, 2005 at 03:45 UTC | |
by BrowserUk (Patriarch) on Oct 09, 2005 at 04:33 UTC | |
| |
Re: lhs substr(): refs vs. scalars
by Corion (Patriarch) on Oct 08, 2005 at 16:36 UTC | |
Re: lhs substr(): refs vs. scalars
by pg (Canon) on Oct 08, 2005 at 16:45 UTC |