[PLUG] Re: Perl

Wil Cooley wcooley at nakedape.cc
Sat Jul 8 00:39:34 UTC 2006


On Fri, 2006-07-07 at 13:41 -0700, Randal L. Schwartz wrote:

> Well, whether you spell it as a plain character or a regex, it gets *turned*
> *into* a regex.  For example,
> 
>         @list = split ".", "127.0.0.1";
> 
> probably doesn't do what you want, because Perl treats it *identical* to:
> 
>         @list = split /./, "127.0.0.1";
> 
> Hence, I *always* write the first arg as a regex, to ensure that my reader
> understands that whether I write it as a regex or not, it IS a regex.

Oh yeah, I've been meaning to follow up--I RTFM'd and realized that I
was wrong that it would behave differently.  Well, actually, it
does--there's overhead in turning the string to pattern, and it's
especially bad for regex metacharacters:

----------------
Timing splitting of 'a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z'...
                Rate split_ch_dot split_re_dot
split_ch_dot 31439/s           --         -32%
split_re_dot 45948/s          46%           --
Timing splitting of 'a:b:c:d:e:f:g:h:i:j:k:l:m:n:o:p:q:r:s:t:u:v:w:x:y:z'...
                  Rate split_ch_colon split_re_colon
split_ch_colon 45081/s             --           -11%
split_re_colon 50716/s            12%             --
----------------

#!/usr/bin/perl
use strict;
use warnings;
use Benchmark qw( cmpthese );
use Data::Dumper;

my $string_to_split = join('.', 'a'..'z');
my $string_to_split2 = join(':', 'a'..'z');

print "Timing splitting of '$string_to_split'...\n";

cmpthese -1, {
    split_re_dot => sub { my @x = split /\./, $string_to_split; return; },
    split_ch_dot => sub { my @x = split '.', $string_to_split; return; },
};

print "Timing splitting of '$string_to_split2'...\n";

cmpthese -1, {
    split_re_colon => sub { my @x = split /\:/, $string_to_split2; return; },
    split_ch_colon => sub { my @x = split ':', $string_to_split2;  return; },
};


Wil
-- 
Wil Cooley <wcooley at nakedape.cc>
Naked Ape Consulting, Ltd. <http://nakedape.cc>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://lists.pdxlinux.org/pipermail/plug/attachments/20060707/d9265080/attachment.asc>


More information about the PLUG mailing list