Your skill will accomplish
what the force of many cannot
lhs substr(): refs vs. scalarsby renodino (Curate)
|on Oct 08, 2005 at 16:30 UTC||Need Help??|
renodino has asked for the
wisdom of the Perl Monks concerning the following question:
This performance question has pestered me for awhile, so I'm hoping someone can shed light wo/ my having to do deep SV deconstruction:
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.
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):
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.
Updated above code to include a 4 arg substr() direct from params, which seems about 20% faster than an lhs 3 arg substr().