Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re^3: Puzzle: The Ham Cheese Sandwich cut.

by robin (Chaplain)
on Nov 21, 2005 at 19:28 UTC ( #510535=note: print w/replies, xml ) Need Help??

in reply to Re^2: Puzzle: The Ham Cheese Sandwich cut.
in thread Puzzle: The Ham Cheese Sandwich cut.

Hmm, that's interesting. But I bet the goto is faster if @_ has, say a million elements. You're saving an awful lot of copying. Update: I lost this bet :-)

It also saves a fair amount of stack space.

  • Comment on Re^3: Puzzle: The Ham Cheese Sandwich cut.

Replies are listed 'Best First'.
Re^4: Puzzle: The Ham Cheese Sandwich cut.
by Roy Johnson (Monsignor) on Nov 21, 2005 at 19:31 UTC
    It isn't the goto that saves the copying. It's the use of & without parentheses to call the function. As far as I can tell, the goto is a useless relic.

    Update: Ok, not entirely useless: if you want a routine not to return to where you called it from, goto is the way to do it. In other words, use it when you want to do weird control flow (which is what goto is all about). Example:

    sub one { print "In one\n"; goto &two; print "never print this if you goto\n"; } sub two { print "In two\n"; } one();
    It's like exec for the subroutine domain.

    Caution: Contents may have been coded under pressure.
      In fact I'm wrong in any case. A naive recursive call seems to be just a hair faster than the goto, even when finding the median of a million-element list.

      As you suggest, the fastest of all is to simply call the sub with an ampersand, but really the difference is very small. I wonder whether this is running into unshift's worst-case performance.

      Premature optimization and all thatů

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://510535]
[Corion]: Also, there's one colleague who wants to join a well-established weekly telco, and I'm not convinced of the use there...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2017-03-29 07:43 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (344 votes). Check out past polls.