Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Deep structure access and temp variable

Reply
Thread Tools

Deep structure access and temp variable

 
 
Adrien BARREAU
Guest
Posts: n/a
 
      06-13-2013
Hi all.


A simple question, but I have no clue about it:

# Version 1
my $A = $stuff->{a}{b}{c}{d}{e};
my $B = $stuff->{a}{b}{c}{d}{f};
my $C = $stuff->{a}{b}{c}{d}{g};

# Version 2

my $tmp = $stuff->{a}{b}{c}{d};
my $A = $tmp->{e};
my $B = $tmp->{f};
my $C = $tmp->{g};


What is the most efficient?
What appears to be the most idiomatic?


I don't have the knowledge about Perl guts to properly guess here.
Of course, it does not have be only HASHes refs in the structure.


Adrien.
 
Reply With Quote
 
 
 
 
Rainer Weikusat
Guest
Posts: n/a
 
      06-13-2013
Adrien BARREAU <(E-Mail Removed)> writes:
> A simple question, but I have no clue about it:
>
> # Version 1
> my $A = $stuff->{a}{b}{c}{d}{e};
> my $B = $stuff->{a}{b}{c}{d}{f};
> my $C = $stuff->{a}{b}{c}{d}{g};
>
> # Version 2
>
> my $tmp = $stuff->{a}{b}{c}{d};
> my $A = $tmp->{e};
> my $B = $tmp->{f};
> my $C = $tmp->{g};
>
>
> What is the most efficient?


This depends on the relative cost of the additional assignment versus
that of the dereferencing chain. I usually follow the rule of thumb
that using an intermediate variable only makes sense if it is going to
be used for at least three times.

But there's a more important concern: Readiblity of the code and the
ease with which it can be modified: Both suffer in case of 'obnoxious
repetition', as in your first example. Also, the intermediate variable
could have a more sensible name than $tmp which could communicate its
nature better the $stuff->{a}{b}{c}{d}.

> What appears to be the most idiomatic?


Is you're asking about "what everyone else does" you can rest assured
that no problem which can be solved with copy'n'paste will be solved
with anything but copy'n'paste because this minimizes that time of
each individual 'trivial work unit' (also, people greatly prefer
moving over thingking).
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      06-13-2013
Adrien BARREAU <(E-Mail Removed)> wrote:
>A simple question, but I have no clue about it:
>
># Version 1
>my $A = $stuff->{a}{b}{c}{d}{e};
>my $B = $stuff->{a}{b}{c}{d}{f};
>my $C = $stuff->{a}{b}{c}{d}{g};
>
># Version 2
>
>my $tmp = $stuff->{a}{b}{c}{d};
>my $A = $tmp->{e};
>my $B = $tmp->{f};
>my $C = $tmp->{g};
>
>What is the most efficient?


Most efficient in regard to what? No, I'm not joking but I really mean
it. I'm guessing you are referring to runtime but that is by no means
the rule-it-all. Actually, if you program is so slow that you have to
optimize on this micro-level, then you should look for a better
algorithm or a different programming language.

Having said that I would guess the runtime depends upon how large $tmp
is. In version 2 you are creating a copy of the data. This can be 'slow'
for large data. If you want a trustworthy answer you will have to
benchmark both versions using your(!) live date.

But that really doesn't matter that much because ....

>What appears to be the most idiomatic?


.... much more important than runtime micro-optimization is
maintainability of your code. And assuming that you can find a more
meaningful name than $tmp, then the second version wins hands-down in
that department.
And that kind of efficiency wins in the long run.

jue
 
Reply With Quote
 
Adrien BARREAU
Guest
Posts: n/a
 
      06-13-2013
On 06/13/2013 05:55 PM, Ben Morrow wrote:
>
> Quoth Adrien BARREAU<(E-Mail Removed)>:
>>
>> A simple question, but I have no clue about it:
>>

[cut]
>>
>> What is the most efficient?

>
> Who cares? (Seriously, unless you've determined that you have a
> performance problem and that this bit of code is responsible, it's not
> worth compromising readability.)
>


Well, I do. I would not ask if I were not, would I?
I'm working on some piece of code which must run as fast as possible (in
Perl limits), hence the question.
And a bit of curiosity, too.

>> What appears to be the most idiomatic?

>
> The second. One of the more important rules of programming is DRY:
> 'Don't Repeat Yourself'. The first repeats a whole lot of stuff
> unnecessarily.


I agree.
That what I do for now. That was just in case.


Thanks .


Adrien.
 
Reply With Quote
 
Adrien BARREAU
Guest
Posts: n/a
 
      06-13-2013
On 06/13/2013 05:18 PM, Rainer Weikusat wrote:

>> What is the most efficient?

>
> This depends on the relative cost of the additional assignment versus
> that of the dereferencing chain. I usually follow the rule of thumb
> that using an intermediate variable only makes sense if it is going to
> be used for at least three times.
>
> But there's a more important concern: Readiblity of the code and the
> ease with which it can be modified: Both suffer in case of 'obnoxious
> repetition', as in your first example. Also, the intermediate variable
> could have a more sensible name than $tmp which could communicate its
> nature better the $stuff->{a}{b}{c}{d}.
>
>> What appears to be the most idiomatic?

>
> Is you're asking about "what everyone else does" you can rest assured
> that no problem which can be solved with copy'n'paste will be solved
> with anything but copy'n'paste because this minimizes that time of
> each individual 'trivial work unit' (also, people greatly prefer
> moving over thingking).



ACK.

Thanks .

Adrien.
 
Reply With Quote
 
Adrien BARREAU
Guest
Posts: n/a
 
      06-13-2013
[cut]

>> What is the most efficient?

>
> Most efficient in regard to what? No, I'm not joking but I really mean
> it. I'm guessing you are referring to runtime but that is by no means
> the rule-it-all. Actually, if you program is so slow that you have to
> optimize on this micro-level, then you should look for a better
> algorithm or a different programming language.
>
> Having said that I would guess the runtime depends upon how large $tmp
> is. In version 2 you are creating a copy of the data. This can be 'slow'
> for large data. If you want a trustworthy answer you will have to
> benchmark both versions using your(!) live date.
>
> But that really doesn't matter that much because ....
>
>> What appears to be the most idiomatic?

>
> ... much more important than runtime micro-optimization is
> maintainability of your code. And assuming that you can find a more
> meaningful name than $tmp, then the second version wins hands-down in
> that department.
> And that kind of efficiency wins in the long run.
>


ACK.

Thanks .

Adrien.
 
Reply With Quote
 
Jim Gibson
Guest
Posts: n/a
 
      06-13-2013
In article <kpcm7k$ra8$(E-Mail Removed)>, Adrien BARREAU
<(E-Mail Removed)> wrote:

> A simple question, but I have no clue about it:
>
> # Version 1
> my $A = $stuff->{a}{b}{c}{d}{e};
> my $B = $stuff->{a}{b}{c}{d}{f};
> my $C = $stuff->{a}{b}{c}{d}{g};
>
> # Version 2
>
> my $tmp = $stuff->{a}{b}{c}{d};
> my $A = $tmp->{e};
> my $B = $tmp->{f};
> my $C = $tmp->{g};
>
> What is the most efficient?
> What appears to be the most idiomatic?
>
> I don't have the knowledge about Perl guts to properly guess here.
> Of course, it does not have be only HASHes refs in the structure.


There is no need to guess. Perl has the Benchmark module that makes
doing these kind of comparisons relatively easy:

#!/usr/bin/perl
use warnings;
use strict;
use Benchmark qw(cmpthese);

my $stuff = { a => { b => { c => { d => { e => 1, f => 2, g => 3 }}}}};

cmpthese( 1_000_000, {
'Deep' => sub {
my $x = $stuff->{a}{b}{c}{d}{e};
my $y = $stuff->{a}{b}{c}{d}{f};
my $z = $stuff->{a}{b}{c}{d}{g};
},
'Temp' => sub {
my $tmp = $stuff->{a}{b}{c}{d};
my $x = $tmp->{e};
my $y = $tmp->{f};
my $z = $tmp->{g};
}});
__END__

Results from five runs:

41% perl barreau.pl
Rate Deep Temp
Deep 591716/s -- -63%
Temp 1612903/s 173% --
42% perl barreau.pl
Rate Deep Temp
Deep 621118/s -- -32%
Temp 917431/s 48% --
43% perl barreau.pl
Rate Deep Temp
Deep 854701/s -- -20%
Temp 1063830/s 24% --
44% perl barreau.pl
Rate Deep Temp
Deep 740741/s -- -24%
Temp 970874/s 31% --
45% perl barreau.pl
Rate Deep Temp
Deep 793651/s -- -17%
Temp 961538/s 21% --

That is a lot of variation from run to run, but the Temp approach is
clearly faster, which I would expect. It requires seven hash lookups
instead of fifteen.

As far as "idiomatic", I wouldn't worry about it. One person's
idiomatic is another person's idiotic.

--
Jim Gibson
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      06-13-2013
Jim Gibson <(E-Mail Removed)> wrote:
>In article <kpcm7k$ra8$(E-Mail Removed)>, Adrien BARREAU
><(E-Mail Removed)> wrote:
>
>> A simple question, but I have no clue about it:
>>
>> # Version 1
>> my $A = $stuff->{a}{b}{c}{d}{e};
>> my $B = $stuff->{a}{b}{c}{d}{f};
>> my $C = $stuff->{a}{b}{c}{d}{g};
>>
>> # Version 2
>>
>> my $tmp = $stuff->{a}{b}{c}{d};
>> my $A = $tmp->{e};
>> my $B = $tmp->{f};
>> my $C = $tmp->{g};
>>
>> What is the most efficient?
>> What appears to be the most idiomatic?
>>
>> I don't have the knowledge about Perl guts to properly guess here.
>> Of course, it does not have be only HASHes refs in the structure.

>
>There is no need to guess. Perl has the Benchmark module that makes
>doing these kind of comparisons relatively easy:
>

[...]

A very good example for "Wer mißt mißt Mist" (literal: "who measures,
measures manure").

>That is a lot of variation from run to run, but the Temp approach is
>clearly faster, which I would expect. It requires seven hash lookups
>instead of fifteen.


Completely irrelevant because your sample code is not using the OPs
actual live data or at the very least a representative sample of his
live data.
And depending on the size of $tmp this will make a big difference.

jue
 
Reply With Quote
 
Peter J. Holzer
Guest
Posts: n/a
 
      06-13-2013
On 2013-06-13 16:06, Jürgen Exner <(E-Mail Removed)> wrote:
> Adrien BARREAU <(E-Mail Removed)> wrote:
>>A simple question, but I have no clue about it:
>>
>># Version 1
>>my $A = $stuff->{a}{b}{c}{d}{e};
>>my $B = $stuff->{a}{b}{c}{d}{f};
>>my $C = $stuff->{a}{b}{c}{d}{g};
>>
>># Version 2
>>
>>my $tmp = $stuff->{a}{b}{c}{d};
>>my $A = $tmp->{e};
>>my $B = $tmp->{f};
>>my $C = $tmp->{g};
>>
>>What is the most efficient?

>
> Most efficient in regard to what? No, I'm not joking but I really mean
> it. I'm guessing you are referring to runtime but that is by no means
> the rule-it-all. Actually, if you program is so slow that you have to
> optimize on this micro-level, then you should look for a better
> algorithm or a different programming language.


Agreed (mostly).


> Having said that I would guess the runtime depends upon how large $tmp
> is. In version 2 you are creating a copy of the data.


$tmp is reference, so it's always the same size.

hp


--
_ | Peter J. Holzer | Fluch der elektronischen Textverarbeitung:
|_|_) | Sysadmin WSR | Man feilt solange an seinen Text um, bis
| | | http://www.velocityreviews.com/forums/(E-Mail Removed) | die Satzbestandteile des Satzes nicht mehr
__/ | http://www.hjp.at/ | zusammenpaßt. -- Ralph Babel
 
Reply With Quote
 
Ivan Shmakov
Guest
Posts: n/a
 
      06-13-2013
>>>>> Adrien BARREAU <(E-Mail Removed)> writes:

[...]

> # Version 2


> my $tmp = $stuff->{a}{b}{c}{d};
> my $A = $tmp->{e};
> my $B = $tmp->{f};
> my $C = $tmp->{g};


> What is the most efficient? What appears to be the most idiomatic?


FWIW (and now that the experts have said their word), my
personal preference would be to write it as follows:

my $tmp = $stuff->{a}{b}{c}{d};
my ($A, $B, $C) = @$tmp{qw (e f g)};

[...]

--
FSF associate member #7257
 
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.36 Why can't I use "C:\temp\foo" in DOS paths? Why doesn't `C:\temp\foo.exe` work? PerlFAQ Server Perl Misc 0 04-26-2011 10:00 AM
Deep Freeze In Deep Trouble johntangelo@gmail.com Computer Security 3 10-25-2005 11:49 PM
How to initialize member reference with temp object and delete the temp object when the destructor is called? PengYu.UT@gmail.com C++ 2 05-09-2005 05:48 PM
File::Temp: opening the temp. file in "r+" mode? Also "man in the middle" A. Farber Perl Misc 3 03-03-2004 12:42 PM
Temp Internet and temp files now in winnt directory w2k pro Bibble Bobble Computer Support 1 11-27-2003 06:14 PM



Advertisments