Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Temp variable performace vs Inline behavior

by BrowserUk (Patriarch)
on Jun 27, 2012 at 15:05 UTC ( [id://978689]=note: print w/replies, xml ) Need Help??


in reply to Temp variable performace vs Inline behavior

Apart from the fact that, as posted, your two snippets do different things (and the second is missing a ')'), I'd do that this way:

my @devices = grep{ defined( $_ = parse_node( $_ ) ) } @nodes;

With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

The start of some sanity?

Replies are listed 'Best First'.
Re^2: Temp variable performance vs Inline behavior
by Athanasius (Archbishop) on Jun 27, 2012 at 15:37 UTC

    BrowserUk’s solution:

    my @devices = grep{ defined( $_ = parse_node( $_ ) ) } @nodes;

    is compact and efficient, but has one side-effect: the assignment to $_ clobbers the contents of @nodes.

    In cases where this is undesirable, the following seems to do the same without the side-effect:

    my @devices = grep { defined } map { parse_node( $_ ) } @nodes;

    Or is there a simpler way?

    Athanasius <°(((><contra mundum

      You could use the same technique but clobber a temporary array instead...

      my @devices = grep { defined($_ = parse_node($_)) } my @tmp = @nodes;

      Or you could use map:

      my @devices = map { my $x; defined($x=parse_node($_)) ? $x : () } @nodes;

      If parse_node was written to return the empty list in cases of failure (rather than returning the scalar undef), then it would just be:

      my @devices = map { parse_node($_) } @nodes;
      perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re^2: Temp variable performace vs Inline behavior
by Ransom (Beadle) on Jun 27, 2012 at 15:15 UTC
    Whoops about the typo. I don't understand how they are different?

    I've tested my script with both and get the same results. parse_node return a nodelet on success or undef on failure. Where is the difference?

      Calling parse_node() twice in order to avoid a temporary variable, is very unlikely to effect a performance gain.

      Unless parse_node() does almost nothing, in which case you would certainly gain more by in-lining it code in the loop. The grep solution will almost certainly prove faster.

      Either way, it probably isn't worth the effort unless this is likely to be called thousands or millions of times.

      My philosophy is "Optimise if it is worth it!". And that means concentrating ones effort where gains are likely to be measurable -- in the overall runtime.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      The start of some sanity?

      Where is the difference?

      In the number of times parse_node is called, for starters.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://978689]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-20 00:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found