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

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

by Anonymous Monk
on Aug 19, 2002 at 18:48 UTC ( #191275=note: print w/replies, xml ) Need Help??


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

Those two code snippets are not at all similar in function, so benchmarking them is useless.
  • Comment on Re: Re: Optimizing existing Perl code (in practise)

Replies are listed 'Best First'.
Re: Re: Re: Optimizing existing Perl code (in practise)
by feloniousMonk (Pilgrim) on Aug 20, 2002 at 14:04 UTC
    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 --

      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.

      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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (11)
As of 2019-05-20 15:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (128 votes). Check out past polls.

    Notices?