Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: next unless condition

by learnedbyerror (Monk)
on Mar 31, 2016 at 22:15 UTC ( [id://1159233]=note: print w/replies, xml ) Need Help??


in reply to Re: next unless condition
in thread next unless condition

Over the last 20 years, I have seen this construct written in the very perlish manner of :

$cell || next;

which is the same as:

$cell or next;

The explanation that I have "heard" in the past is that this performed faster than other functionally equivalent alternatives. I have always been skeptical of this POV and too lazy to actually test it

I personally like the way next unless $cell reads for loop control and often use this construct. In general, I am not a big fan of unless and general use If ( not $cell ) with the exception of loop control. My rationale behind this is that I think it is easier to read and comprehend for someone who comes behind me to maintain any code that I have created. Please note the use of belief here. I do not purport that to be a fact.

lbe

Replies are listed 'Best First'.
Re^3: next unless condition
by ikegami (Patriarch) on Apr 01, 2016 at 15:51 UTC

    The explanation that I have "heard" in the past is that this performed faster than other functionally equivalent alternatives.

    That's not the case.

    $ perl -MO=Concise,-exec -e'f() unless $x' 1 <0> enter 2 <;> nextstate(main 1 -e:1) v:{ 3 <#> gvsv[*x] s 4 <|> or(other->5) vK/1 5 <0> pushmark s 6 <#> gv[*f] s/EARLYCV 7 <1> entersub[t3] vKS/TARG,1 8 <@> leave[1 ref] vKP/REFC -e syntax OK $ perl -MO=Concise,-exec -e'$x or f()' 1 <0> enter 2 <;> nextstate(main 1 -e:1) v:{ 3 <#> gvsv[*x] s 4 <|> or(other->5) vK/1 5 <0> pushmark s 6 <#> gv[*f] s/EARLYCV 7 <1> entersub[t4] vKS/TARG,1 8 <@> leave[1 ref] vKP/REFC -e syntax OK
Re^3: next unless condition
by dsheroh (Monsignor) on Apr 01, 2016 at 07:51 UTC
    There is a difference in operator precedence between $cell || next and $cell or next, so they're not quite the same. While they're effectively identical in this case, even the minor modification of adding an assignment results in $x = $cell || next and $x = $cell or next behaving differently.

    The efficiency explanation you've heard is accurate when dealing with extremely-close-to-the-metal languages and non-optimizing compilers, since testing whether a memory location already holds the value 0 is one of the fastest tests you can do at the machine level. But optimizing compilers are smart enough to figure out for themselves that if (not $cell) ... is equivalent to $cell || ... while still allowing you to write it the more readable way, and Perl is so far from the metal that these kinds of concerns are completely inapplicable anyhow.

      I agree that you should write the clearest HLL code possible. At the local college I work with ASM students and sometimes we play "beat the compiler". With the right example, even a beginning student can do this vs the highest optimization level. This example would not be a good one because if (not $cell) ... and $cell || ... will code every similarly even with a "dumb" compiler. They are essentially the same. Load from memory, ALU operation to set flags, jump on flags that were just set. The ALU operation could be a or eax,eax(nothing but set flags) or  neg eax (negate and set flags). Different kind of jump instruction required but timing is the same. I wouldn't worry about this at all.

      A bigger mistake in source coding is to assume that shorter source code maybe with a bunch of fancy maps, joins, and other functions is faster than longer code that is more straightforward. You can write one line in HLL that takes a lot more work at the low level that something else that takes many HLL lines.

        Load from memory, ALU operation to set flags, jump on flags that were just set.

        FWIW, in some CPUs, the load instructions will also set/clear the "zero" and "negative" flags.

        You are right. As a rule, today's optimizing compilers are very good at producing (near) best machine code.

        With Perl, I'd say just write the most understandable and maintainable code you can.

Re^3: next unless condition
by haukex (Archbishop) on Apr 01, 2016 at 07:44 UTC

    Hi lbe,

    Yes, $cell || next; is yet another one to add to the list of ways to do it :-) (Although it's probably easier to get bitten by precedence issues on that one so personally, I'd avoid it.)

    if (...) {...} is definitely what I'd use if the condition or the statement becomes more complex. For example, I've often written code something like if ($x && $y) { warn "skipping b/c foo"; next }

    Regards,
    -- Hauke D

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-20 02:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found