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
--
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.
| [reply] [d/l] [select] |
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?
| [reply] [d/l] |
|