[boulder.pm] unsubscribe

Keanan Smith KSmith at netLibrary.com
Thu Jun 27 17:27:18 CDT 2002


Heh, actually, the first one is slightly broken, it shouldn't contain a
forward-lookahead at all
(I just realized that since I copied that from old, commented out code, it's
behavior should have been
changed to match the new version of the code.)

Here's a simplified version with the names changed to stop warping people's
brains:

  @array = split(/\n/,$value); 
  my $mypos = 0;
  foreach $line (@array)
  {
    $mypos += length($line);
    ...dostuff...
  }

Versus

  while ($value =~ /\n/g)
  {
    my $mypos = pos($value);
    $array[$i] = $1;
    $i++;
    ...dostuff...
  }

(And yes, the second one is still faster, weird eh? At least I think so...)


And change of '$value' and change of the regular expression change the total
time,
but the relationship between the two times remains more-or-less the same

(The second is about 20% faster, in all the cases I tried)

-----Original Message-----
From: Jay Kominek [mailto:Jay.Kominek at colorado.edu]
Sent: Thursday, June 27, 2002 4:01 PM
To: 'boulder-pm-list at happyfunball.pm.org'
Subject: RE: [boulder.pm] unsubscribe



On Thu, 27 Jun 2002, Keanan Smith wrote:

>    @{$me->{arrayrep}} = split(/(?=$me->{break})/,$value);
>    my $mypos = 0;
>    foreach $line (@{$me->{arrayrep}})

>     while ($value =~ /$me->{break}/g)
>     {
>       my $mypos = pos($value);

> I would think that the first (Which calls the 'split' builtin to generate
> the array)
> would be faster than the second (Which repeatedly does a regular
expression
> and assigns the list elements by hand)

> But in fact it's the reverse! Weird eh?
> Anyone have a good explination for why?

You're allocating more memory for the first one, as well as using a zero
width assertion. If I recall correctly, those (can) significantly increase
the time it takes to perform a match. They'd certainly make it more
complex to repeatedly match a string.

The double indirection to access the elements of the array can't help
much, either. (Hopefully Perl is optimizing it away, but you never know.)

Any actual values for the change in algorithmic and constant time?

- Jay Kominek <jay.kominek at colorado.edu>
  Plus ça change, plus c'est la même chose




More information about the Boulder-pm mailing list