in reply to Re: Label makes a sub to return empty list -- "secret"? documented?
in thread Label makes a sub to return empty list -- "secret"? documented?

Thanks, everyone. FWIW (I usually distrust multi-millions-per-sec benchmarks):

cmpthese 5e7, { 1 => sub { my $x = 1 + 1; return }, 2 => sub { my $x = 1 + 1; () }, 3 => sub { my $x = 1 + 1; _: }, }; 1 27367269/s -- -13% -22% 2 31367629/s 15% -- -11% 3 35161744/s 28% 12% --

"Perl subs are expensive"? The cheapest adjustment is just above. Otoh, now I have a justification (not that I need any or anyone cares) not to follow PBP's "Place the label on the line preceding the loop keyword" -- just in case, easier and safer while debugging and commenting-out this or that, to keep label and code on the same line.

  • Comment on Re^2: Label makes a sub to return empty list -- "secret"? documented?
  • Download Code

Replies are listed 'Best First'.
Re^3: Label makes a sub to return empty list -- "secret"? documented?
by ikegami (Patriarch) on Sep 13, 2025 at 04:09 UTC

    Sometimes, I prefer to invert the numbers to get the time saved to put things into perspective.

    27367269 calls/s = 37 ns/call 31367629 calls/s = 32 ns/call 35161744 calls/s = 28 ns/call

    So _: is 9 ns faster than return (assuming the numbers are meaningful).

    After 100 million calls, you still wouldn't have saved a second by switching from return to _:.

    Update: I had moved a decimal point in the wrong direction. Fixed.

      27367269 calls/s = 0.365 ns/call

      I think your numbers are a couple of orders of magnitude out. By my maths 1 / 27367269 = 0.0000000365, so that's 36.5ns per call.

      Even so, your point is valid. It is only in extreme cases where any of this is really worth worrying about.


      🦛

        The numbers seemed absurd, but I was too tired to register I needed to redo the math. I had moved a decimal point in the wrong direction. Fixed.

Re^3: Label makes a sub to return empty list -- "secret"? documented?
by LanX (Saint) on Sep 13, 2025 at 23:23 UTC
    > Otoh, now I have a justification (not that I need any or anyone cares) not to follow PBP's "Place the label on the line preceding the loop keyword" --

    That's a weird logical construct

    There was no indication or benchmark in this thread so far that placing a label at a previous line comes with a notable performance overhead.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

      It's not performance this time. Intuitively, labels belong to the same "subordinate" "category" as e.g. semicolons, both not far from comments and white-space. Extra semicolon at the end of a block doesn't change what this block evaluates to. Lone label, I now know, does, which is dangerous. Neglected to have been (temporarily?) commented-out along with its loop (if placed per PBP at the next line) and maybe some further statements. Of course, it's the same weird nonsense, please ignore.

        OK I see your point now, but it's a bit contrived to have expectations about the new return value after commenting a trailing loop out.

        (IIRC is the return value of a loop not defined.)

        Anyway thanks for the discussion, I learned something new. :)

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        see Wikisyntax for the Monastery