Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: declaring lexical variables in shortest scope: performance?

by tobyink (Canon)
on Mar 31, 2020 at 10:54 UTC ( #11114843=note: print w/replies, xml ) Need Help??

in reply to declaring lexical variables in shortest scope: performance?

As well as what others have said, remember that Perl does have optimizations in place for common idioms. For example

my @things = ( ... ); # some list, whatever @things = sort @things;

You may think that sort gets passed @things, then returns the sorted list of things, and that gets assigned back to the @things array as a list assignment. But you'd be wrong. Perl notices that you're sorting an array and assigning it back to itself, and uses an optimized code path that doesn't involve having to build a new list and do list assignment; it does an in-place sort.

Common idioms do get optimized for when possible, so there are benefits to sticking with them.

With for my $var (...) {...}, Perl knows that $var won't be leaking outside the body of the loop, so can at least potentially optimize based on that.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2020-06-01 17:22 GMT
Find Nodes?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?

    Results (5 votes). Check out past polls.