Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: Re: Re: Optimizing existing Perl code (in practise)

by feloniousMonk (Pilgrim)
on Aug 20, 2002 at 14:04 UTC ( #191459=note: print w/replies, xml ) Need Help??

in reply to Re: Re: Optimizing existing Perl code (in practise)
in thread Optimizing existing Perl code (in practise)

Um, they do perform the same function. They both place 2 variables
into an array....

Yes, the method is different but what I intended to illustrate is that for a given set of data,
2 different methods of processing may have significant performance differences
while giving the same results.

Also implicit in the code is that the solution will not work everywhere, which is why optimization depends on what
you intend on optimizing.

-felonious --
  • Comment on Re: Re: Re: Optimizing existing Perl code (in practise)

Replies are listed 'Best First'.
Re^4: Optimizing existing Perl code (in practise)
by Aristotle (Chancellor) on Aug 20, 2002 at 15:27 UTC

    No they don't. For starters, your split produces and assigns at least three values in every case the pattern matches. The difference in their effects may be irrelevant to your specific application, but that doesn't make them equivalent. Taking that into consideration from the start, you shouldn't have needed to benchmark them to predict the outcome.

    If you want a regex version that works meaningfully similar to the split, it would have to look something like this: my @a = ($foo =~ /(?:\s+)?(.*?)(?=\s)/g);
    (Because your pattern is as simple as \s+, you can formulate a regex version like my @a = ($foo =~ /(\S+)/g);
    but that doesn't generalize to splitting at foo(?:bar|baz)? )

    Makeshifts last the longest.

Re: Re: Re: Re: Optimizing existing Perl code (in practise)
by Anonymous Monk on Aug 20, 2002 at 15:57 UTC
    No the do not perform the same function. Your re method functions as a gaurd clause allowing for a) early failure and b) avoiding assignment on failure. The split version performs the assignment even if the strings do not match the pattern. If your data is always going to pass the re, then the split version would be the faster version (and even better than your split version would be  split " ", $foo). Care to show your benchmark where the re version was 90% faster?

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2019-07-19 10:46 GMT
Find Nodes?
    Voting Booth?

    No recent polls found