Your tests are bad. The only copying is happening outside of the functions you are testing. Specifically, they occurring when you assign the string to $tmp in $tmp = out1() and in $tmp = fun1($str).
Right now, you have
my $str = out1(); my $ref = out2(); my $str = fun1($str); my $ref = fun2(\$str);
If you wanted to fix the test, you'd use
my $str = out1(); my $str = ${ out2() }; my $str = fun1($str); my $str = ${ fun2(\$str) };
or
my $ref = \out1(); my $ref = out2(); my $ref = \fun1($str); my $ref = fun2(\$str);
As you can see, passing a string by reference seems to slow down (in2 is slower than in1)
Perl always passes by reference. in2 passes *a* reference (by reference). This reference has to be created, dereferenced and destroyed. This is all work that in2 does on top of what in1 does, so of course it will be slower.
Update: The last paragraph was saying "out1" and "out2" when I meant "in1" and "in2". Fixed.
In reply to Re: How to avoid string copies in function calls?
by ikegami
in thread How to avoid string copies in function calls?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |