Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Does Perl micro-optimize?

Reply
Thread Tools

Does Perl micro-optimize?

 
 
rihad
Guest
Posts: n/a
 
      10-10-2007
Hello there,

Say we have a $hash:

if ($hash{'A-String-Of-Any-Length'} eq 'foo') { ... }
else if ($hash{'A-String-Of-Any-Length'} eq 'bar') { ... }

Would Perl notice that hash lookup needs to be resolved only once, and
optimize further accesses? Or does one need to explicitly use a $tmp
for that?

Thanks.

 
Reply With Quote
 
 
 
 
Mirco Wahab
Guest
Posts: n/a
 
      10-10-2007
rihad wrote:
> Hello there,
>
> Say we have a $hash:
>
> if ($hash{'A-String-Of-Any-Length'} eq 'foo') { ... }
> else if ($hash{'A-String-Of-Any-Length'} eq 'bar') { ... }
>
> Would Perl notice that hash lookup needs to be resolved only once, and
> optimize further accesses? Or does one need to explicitly use a $tmp
> for that?


Perl5 does not, you can check that out for yourself
by the following snippet:

my %hash = ('A_String_Of_Any_Length', 'FUD', map +(rand,rand),1..1000 );

1: if( $hash{A_String_Of_Any_Length} eq 'foo') {
print "do a lot of thinngs\n"
}
2: elsif( $hash{A_String_Of_Any_Length} eq 'bar') {
print "do not so simple things\n"
}
else {
print "do nothing\n"
}


if viewed by perl -MO=Bblock rihad.pl,
it shows both hash lookups:
...
OP (0x1838050) padhv [1]
1: SVOP (0x1838030) const [14] PV (0x1969814) "A_String_Of_Any_Length"
BINOP (0x183800c) helem
SVOP (0x1837fec) const [15] PV (0x196982c) "foo"
BINOP (0x1837fc seq

LOGOP (0x1837b40) cond_expr
OP (0x1837f0 pushmark
SVOP (0x1837f4 const [16] PV (0x1969820) "do a lot of thinngs\n"
LISTOP (0x1837f24) print

OP (0x1837e8c) padhv [1]
2: SVOP (0x1837e6c) const [17] PV (0x196983 "A_String_Of_Any_Length"
BINOP (0x1837e4 helem
SVOP (0x1837e2 const [18] PV (0x1969850) "bar"
BINOP (0x1837e04) seq

LOGOP (0x1837b84) cond_expr
...

Regards

M.
 
Reply With Quote
 
 
 
 
Ben Morrow
Guest
Posts: n/a
 
      10-10-2007

Quoth rihad <(E-Mail Removed)>:
> Hello there,
>
> Say we have a $hash:
>
> if ($hash{'A-String-Of-Any-Length'} eq 'foo') { ... }
> else if ($hash{'A-String-Of-Any-Length'} eq 'bar') { ... }
>
> Would Perl notice that hash lookup needs to be resolved only once, and
> optimize further accesses?


Nope.

> Or does one need to explicitly use a $tmp for that?


Yup. Note that under some circumstances

my $tmp = $hash{foo};

can be slower than redoing the lookup, notably if $hash{foo} contains a
long string that must be copied. This can be got around with refs, but
that's messy. As ever, profile before you start thinking about
optimizing.

Note also that perl couldn't possibly perform this optimization, as it
would break tied hashes, or, at any rate, make the semantics highly
unreliable.

Ben

 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      10-10-2007
rihad <(E-Mail Removed)> wrote:
> Hello there,
>
> Say we have a $hash:
>
> if ($hash{'A-String-Of-Any-Length'} eq 'foo') { ... }
> else if ($hash{'A-String-Of-Any-Length'} eq 'bar') { ... }


Perl doesn't have an "else if". It has "elsif"

> Would Perl notice that hash lookup needs to be resolved only once, and
> optimize further accesses?


I don't think so, but if it did do this kind of thing ever, it would
probably vary a lot from situation to situation and version to version.
And imagine the chaos this could cause for tied hashes.

If the look-up string were in a variable, I know that in some situations
and with some versions, the hashed integer of the (potentially very large)
string can be stored in the SV rather than needing to be recomputed. I
don't know what happens with literals.

> Or does one need to explicitly use a $tmp
> for that?


If one wants the lookup not to be done twice, then you would need to use
a temp variable. It seems that this is at least as likely to make things
slower as faster, though.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      10-10-2007
On Wed, 10 Oct 2007 10:23:01 -0700, rihad <(E-Mail Removed)> wrote:

>if ($hash{'A-String-Of-Any-Length'} eq 'foo') { ... }
>else if ($hash{'A-String-Of-Any-Length'} eq 'bar') { ... }


If you have many such chained elsif's, then you *may* want to go the
road of HoH. I seldom if ever use explicit elsif's.

>Would Perl notice that hash lookup needs to be resolved only once, and
>optimize further accesses? Or does one need to explicitly use a $tmp
>for that?


Sometimes I like to exploit C<for>'s aliasing properties for this
kinda things. Such use is occasionally controversial.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
rihad
Guest
Posts: n/a
 
      10-11-2007
On Oct 10, 10:41 pm, Ben Morrow <(E-Mail Removed)> wrote:
> Quoth rihad <(E-Mail Removed)>:
> my $tmp = $hash{foo};
>
> can be slower than redoing the lookup, notably if $hash{foo} contains a
> long string that must be copied.


Why would Perl want to copy the value? Doesn't it do copy-on-write or
similar?

 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      10-11-2007
rihad <(E-Mail Removed)> wrote:
> On Oct 10, 10:41 pm, Ben Morrow <(E-Mail Removed)> wrote:
>> Quoth rihad <(E-Mail Removed)>:
>> my $tmp = $hash{foo};
>>
>> can be slower than redoing the lookup, notably if $hash{foo} contains a
>> long string that must be copied.

>
> Why would Perl want to copy the value?



Because you told it to, via an assignment operator.


> Doesn't it do copy-on-write or
> similar?



The code above is doing a write (to the $tmp variable).


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      10-11-2007

Quoth Tad McClellan <(E-Mail Removed)>:
> rihad <(E-Mail Removed)> wrote:
> > On Oct 10, 10:41 pm, Ben Morrow <(E-Mail Removed)> wrote:
> >> Quoth rihad <(E-Mail Removed)>:
> >> my $tmp = $hash{foo};
> >>
> >> can be slower than redoing the lookup, notably if $hash{foo} contains a
> >> long string that must be copied.

> >
> > Why would Perl want to copy the value?

>
> Because you told it to, via an assignment operator.
>
> > Doesn't it do copy-on-write or
> > similar?

>
> The code above is doing a write (to the $tmp variable).


There is no good reason why perl couldn't copy-on-write the scalar: that
is, make a new SV that points at the same string buffer until one of
them is modified. There is some support for this in 5.9.x, but I don't
think it's ready to be used yet (?).

Ben

 
Reply With Quote
 
rihad
Guest
Posts: n/a
 
      10-11-2007
On Oct 11, 5:43 pm, Ben Morrow <(E-Mail Removed)> wrote:
> There is some support for this in 5.9.x, but I don't
> think it's ready to be used yet (?).
>


Noo, you've got to be kidding... At least subroutine arguments are
always passed by reference in Perl (which is good for speed, but bad
if you unintentionally modify them).

On a side note, and I won't be the first one to say this: Perl is
difficult. Too many special cases. Or maybe I have this impression
because Programming Perl by Larry Wall is very hard reading.

 
Reply With Quote
 
Mirco Wahab
Guest
Posts: n/a
 
      10-11-2007
rihad wrote:
> On Oct 11, 5:43 pm, Ben Morrow <(E-Mail Removed)> wrote:
>> There is some support for this in 5.9.x, but I don't
>> think it's ready to be used yet (?).

>
> Noo, you've got to be kidding... At least subroutine arguments are
> always passed by reference in Perl (which is good for speed, but bad
> if you unintentionally modify them).


In perl, only SV's (pointers to scalar values) or RV's (pointers
to those SV's, to Arrays, Hashes or whatever) are passed into
subroutines. There are "idiomatic expressions" for taking the
value they point to out into (copies) variables,
like: my ($var1, $var2) = @_

> On a side note, and I won't be the first one to say this: Perl is
> difficult. Too many special cases. Or maybe I have this impression
> because Programming Perl by Larry Wall is very hard reading.


Thats true in my opinion, The "LLama Book"
(http://www.oreilly.com/catalog/lperl3/) is
much better for you if you jump into Perl
from somewhere else. The "Camel Book",
(http://www.oreilly.com/catalog/pperl3/index.html)
which is the book you mentioned, will be most
valuable if you are at least somehow "intermediate".

Regards

M.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 5.38 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? PerlFAQ Server Perl Misc 0 03-09-2011 11:00 PM
FAQ 5.38 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? PerlFAQ Server Perl Misc 0 02-11-2011 05:00 AM
FAQ 2.17 What is perl.com? Perl Mongers? pm.org? perl.org? cpan.org? PerlFAQ Server Perl Misc 0 02-03-2011 11:00 AM
FAQ 1.4 What are Perl 4, Perl 5, or Perl 6? PerlFAQ Server Perl Misc 0 01-23-2011 05:00 AM
Perl Help - Windows Perl script accessing a Unix perl Script dpackwood Perl 3 09-30-2003 02:56 AM



Advertisments