Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

always in quest to eliminate temporary variables

by visnu (Sexton)
on Jun 15, 2000 at 22:47 UTC ( [id://18368]=perlquestion: print w/replies, xml ) Need Help??

visnu has asked for the wisdom of the Perl Monks concerning the following question:

can anyone get rid of $pre_next_line in:
($_, $pre_next_line) = split /\n/, $_, 2; $next_line = $pre_next_line . $next_line;

Replies are listed 'Best First'.
RE: always in quest to eliminate temporary variables
by merlyn (Sage) on Jun 16, 2000 at 03:20 UTC
      So simple. So obvious. I wonder how many Monks (including myself) are going to kick themselves for missing that one.
      I thought about that, but I wasn't sure if $1 qualified as a temporary variable.
        Since "eliminate temporary variables" is an artificial (read: meaningless) requirement, it can be interpreted in arbitrary ways.

        -- Randal L. Schwartz, Perl hacker

Re: always in quest to eliminate temporary variables
by Adam (Vicar) on Jun 15, 2000 at 23:40 UTC
    Yes:($_, $next_line) = split /\n/, $_.$next_line, 2;
      sweet.. but i wonder how efficient that is?
        It shouldn't really affect the efficiency much, as you're limitting the split to a maximum of two results returned, and so having a longer string passed to the split won't affect it. Either way, the difference in efficiency will be negligible
        use Benchmark; use strict; timethese(1000000, { 'One Liner' => \&one_liner, 'Two Liner' => \&two_ +liner }); sub one_liner { # Stick all variables in the functions, so we can # see if there's any extra overhead for the extra # variable in two_liner. my $text = q/Here is some test text. I don't really know what I'm typing here, but does it really matter? I don't think so. I'm just kind of hitting random keys and stuff/; my $next_line; ($text, $next_line) = split /\n/, $text.$next_line, 2; } sub two_liner { my $text = q/Here is some test text. I don't really know what I'm typing here, but does it really matter? I don't think so. I'm just kind of hitting random keys and stuff/; my($next_line, $pre_next_line); ($text, $pre_next_line) = split /\n/, $text, 2; $next_line = $pre_next_line . $next_line; } Benchmark: timing 1000000 iterations of One Liner, Two Liner... One Liner: 21 wallclock secs (19.85 usr + 0.00 sys = 19.85 CPU) Two Liner: 22 wallclock secs (22.85 usr + 0.00 sys = 22.85 CPU)
        the difference between the two isn't the split as much as the string concatenations and corresponding memory allocations. the split will split in the same place and do pretty much the exact same thing in both cases (assuming of course that it works its way from the front and not the back as mentioned above).

        all in all, i'd suggest that they're pretty much the same...

        I don't know how efficient (or not efficient) split is. I assume that since split finds the first match in the string (rather then the last) that it isn't using greedy pattern matching. If this is the case, then the matches start at the begining and iterate to the end (whereas greedy goes the other direction) and this means that stuff tacked onto the end of the string won't alter the "efficiency" of the match except in the case where no match is found. I think. I trust that some of the more experienced (and knowledgable) gurus amongst us will correct me if I'm wrong. If I'm right, then there is no efficiency loss with my method.

        By the way, there is no garuntee that your not using a temporary variable will keep one from being created. Compilers are funny things that way.

Re: always in quest to eliminate temporary variables
by ar0n (Priest) on Jun 15, 2000 at 23:41 UTC
    hmmm, how 'bout:
    ($_[0], $_[1]) = split /\n/, $_, 2; $next_line = "$_[1] $next_line";
    update: i take it back, Adams way's better.
    Cool, i learned something!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-24 00:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found