Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

forcing contexts and micro-efficiency

by TomDLux (Vicar)
on Nov 01, 2011 at 16:47 UTC ( #935153=perlquestion: print w/replies, xml ) Need Help??
TomDLux has asked for the wisdom of the Perl Monks concerning the following question:

I'm reviewing the newest Modern Perl and it suggests adding to zero, concatenating to an empty string, and double negation as ways to force numeric, string and boolean context on a value.

Pretty standard stuff, but it made me wonder: Do these simply force a context, or do they carry out the superfluous operation. Would it make sense to detect the pointlessness of the operation beyond forcing context, or would that be more expensive than simply performing the operation?

Micro-optimizers around the world need to know!

As Occam said: Entia non sunt multiplicanda praeter necessitatem.

Replies are listed 'Best First'.
Re: forcing contexts and micro-efficiency
by BrowserUk (Pope) on Nov 01, 2011 at 17:15 UTC

    The answer is always going to be: It depends. Upon many things including what you subsequently do with the variables.

    One trivial test each of ''. and 0+ shows that in these particular cases, the former costs little, and the latter saves a lot:

    cmpthese -1,{ a => q[ my $s = join '', map ''.$_, 1 .. 1e5 ], b => q[ my $s = join '', map $_, 1 .. 1e5 ], };; Rate a b a 12.3/s -- -18% b 14.9/s 21% -- cmpthese -1,{ a => q[ my $s = sum map 0+$_, '0001' .. '1000'], b => q[ my $s = sum map $_, '0001' .. '1000'], };; Rate b a b 1452/s -- -76% a 6056/s 317% --

    I guess whether you think either or both are important will depend upon whether your programs spend most of their time sitting around waiting for someone to click something, or doing real work :)

    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.
Re: forcing contexts and micro-efficiency
by davido (Cardinal) on Nov 01, 2011 at 19:20 UTC

    While you're on the subject you ought to check out Damian Conway's Contextual::Return. It lets you specify explicitly (rather than through obscure tricks) what behavior subs will take under various contexts. The control is very fine grained, even letting you specify boolean behavior, numeric behavior, and so on.

    Documented in its own POD, as well as Perl Best Practices, chapter 9.

    It's probably not useful as a micro-optimization. But 97% of the time you wouldn't want to care about such things anyway. :) And when you run into one of those 3% situations, be sure to document well why an obscure trick has been applied.


      Have you ever looked inside Contextual::Return?

      Or measured the affect it has on the performance of your subroutines and methods?

Re: forcing contexts and micro-efficiency
by chromatic (Archbishop) on Nov 01, 2011 at 21:04 UTC

    As of Perl 5.14, B::Concise shows that the optree contains the appropriate add and concat ops, and the optimizer does not optimize them away.

    Improve your skills with Modern Perl: the free book.

Re: forcing contexts and micro-efficiency
by zentara (Archbishop) on Nov 01, 2011 at 18:12 UTC
    In my zen approach to programming, ;-), intuition tells me this from careful meditation. :-)

    Just doing a simple math or concantation on a variable, is simpler than opening up some object, and doing a change in it. Just from basic principles of information.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://935153]
Approved by Perlbotics
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2018-10-21 05:32 GMT
Find Nodes?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...

    Results (119 votes). Check out past polls.