pwagyi has asked for the wisdom of the Perl Monks concerning the following question:
I am asking this question out of curiosity. Since Perl string is value type, does passing long string as subroutine argument incur performance penalty? I searched around and found that since perl 5.20 (http://perldoc.perl.org/5.20.0/perldelta.html#Performance-Enhancements), Does that mean perl <5.20 will suffer if long strings are passed around? (especially argument is used as read-only)
Perl has a new copy-on-write mechanism that avoids the need to copy the internal string buffer when assigning from one scalar to another. This makes copying large strings appear much faster. Modifying one of the two (or more) strings after an assignment will force a copy internally. This makes it unnecessary to pass strings by reference for efficiency.
# perl string is value type (not reference) my $s = "foo"; my $t = $s; $s .= "bar"; # $t is still 'foo' since assignment copy the value.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: perl string pass by value
by davido (Cardinal) on Apr 02, 2018 at 14:44 UTC | |
by LanX (Saint) on Apr 02, 2018 at 15:30 UTC | |
by pwagyi (Monk) on Apr 03, 2018 at 06:01 UTC | |
by davido (Cardinal) on Apr 03, 2018 at 06:12 UTC | |
Re: perl string pass by value
by ikegami (Patriarch) on Apr 02, 2018 at 16:55 UTC | |
Re: perl string pass by value
by LanX (Saint) on Apr 02, 2018 at 14:29 UTC | |
Re: perl string pass by value
by Anonymous Monk on Apr 02, 2018 at 16:45 UTC | |
by LanX (Saint) on Apr 02, 2018 at 19:02 UTC | |
by Anonymous Monk on Apr 03, 2018 at 04:00 UTC | |
by LanX (Saint) on Apr 03, 2018 at 09:14 UTC | |
by Your Mother (Archbishop) on Apr 03, 2018 at 09:27 UTC |