<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On 16 June 2014 23:09, Olly Betts <span dir="ltr"><<a href="mailto:olly@survex.com" target="_blank">olly@survex.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div id=":1q2" class="" style="overflow:hidden">A Perl scalar holding a string knows the string's length (it doesn't do<br>
something like calling C's strlen() function each time - it can't as the<br>
string might contain zero bytes) so length($foo) just loads a value from<br>
a field in a C structure to get the length.  It may need to turn the<br>
scalar into a string first, but if you call it more than once, only<br>
the first call should need to do that.</div></blockquote></div><br><br></div><div class="gmail_extra">Indeed:<br><br><br></div><div class="gmail_extra">perl  -MO=Concise -e'my $var = q[hello]; length($var)' <br>9  <@> leave[1 ref] vKP/REFC ->(end)<br>
1     <0> enter ->2<br>2     <;> nextstate(main 1 -e:1) v:{ ->3<br>5     <2> sassign vKS/2 ->6<br>3        <$> const(PV "hello") s ->4<br>4        <0> padsv[$var:1,2] sRM*/LVINTRO ->5<br>
6     <;> nextstate(main 2 -e:1) v:{ ->7<br>8     <1> length[t2] vK/1 ->9<br>7        <0> padsv[$var:1,2] s ->8<br><br><br>perl  -MO=Concise,-exec -e'my $var = q[hello]; length($var)' <br>
1  <0> enter <br>2  <;> nextstate(main 1 -e:1) v:{<br>3  <$> const(PV "hello") s<br>4  <0> padsv[$var:1,2] sRM*/LVINTRO<br>5  <2> sassign vKS/2<br>6  <;> nextstate(main 2 -e:1) v:{<br>
7  <0> padsv[$var:1,2] s<br>8  <1> length[t2] vK/1<br>9  <@> leave[1 ref] vKP/REFC<br><br><br>perl -MDevel::Peek -e'my $var = q[hello]; print Dump $var; $var .= q[world]; print Dump $var' <br><br>
SV = PV(0x2559d80) at 0x2579778<br>  REFCNT = 1<br>  FLAGS = (PADMY,POK,IsCOW,pPOK)<br>  PV = 0x257f800 "hello"\0<br>  CUR = 5<br>  LEN = 10<br>  COW_REFCNT = 1<br>SV = PV(0x2559d80) at 0x2579778<br>  REFCNT = 1<br>
  FLAGS = (PADMY,POK,pPOK)<br>  PV = 0x25686a0 "helloworld"\0<br>  CUR = 10<br>  LEN = 16<br><br clear="all"><br></div><div class="gmail_extra">Note value of CUR<br><br></div><div class="gmail_extra">length() does involve a fair amount of low-level jumping, but its more or less a plumbing route that ends up returning the value of CUR<br>
<br><a href="https://metacpan.org/source/RJBS/perl-5.20.0/pp.c#L2933">https://metacpan.org/source/RJBS/perl-5.20.0/pp.c#L2933</a><br><br><a href="https://metacpan.org/source/RJBS/perl-5.20.0/sv.c#L6849">https://metacpan.org/source/RJBS/perl-5.20.0/sv.c#L6849</a><br>
<br><a href="https://metacpan.org/source/RJBS/perl-5.20.0/sv.h#L1666">https://metacpan.org/source/RJBS/perl-5.20.0/sv.h#L1666</a> <br><br></div><div class="gmail_extra"><a href="https://metacpan.org/source/RJBS/perl-5.20.0/sv.h#L1176">https://metacpan.org/source/RJBS/perl-5.20.0/sv.h#L1176</a><br>
<br><a href="https://metacpan.org/source/RJBS/perl-5.20.0/sv.h#L469">https://metacpan.org/source/RJBS/perl-5.20.0/sv.h#L469</a><br><br></div><div class="gmail_extra">Just the question is: Does all that cost more  than creating a variable, assigning it, then re-reading it or not?<br>
<br>That is why its better to optimise based on measured data than hypothesis :) <br><br>---<br><br>use strict;<br>use warnings;<br>use utf8;<br><br>use Benchmark qw( :all :hireswallclock );<br><br>cmpthese(<br>    -10,<br>
    {<br>        '2x length' => sub {<br>            my $value = q[Hello World];<br>            for ( 0 .. 2000 ) {<br>                my $initial = 0;<br>                if ( length($value) > $initial ) {<br>
                    $initial = length($value);<br>                }<br>            }<br>        },<br>        'tempvar' => sub {<br>            my $value = q[Hello World];<br>            for ( 0 .. 2000 ) {<br>
                my $initial = 0;<br>                if ( ( my $temp = length($value) ) > $initial ) {<br>                    $initial = $temp;<br>                }<br>            }<br>          }<br>    }<br>);<br><br>
---<br><br>            Rate   tempvar 2x length<br>tempvar   2323/s        --      -22%<br>2x length 2966/s       28%        --<br>--<br><br></div><div class="gmail_extra">Guess it wasn't worth the effort.<br><br></div>
<div class="gmail_extra">-- <br>Kent<br>
</div></div>