Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org

BBS: Re: Perl string manipulation - September 20, 1998 at 12:02:04

You are here: irt.org | BBS | Re: Perl string manipulation [This BBS is closed]

Posted by Jason Turner on September 20, 1998 at 12:02:04:

In Reply to: Re: Perl string manipulation posted by Jason Nugent on September 19, 1998 at 15:43:41:

Hi Jase,

I'm glad you asked about time. I've changed my mind about split now for this one :-)

Take a look at the following,

try0 is a literal match on everything but the variable - Fastest for Bob's data.

try1 maches from the rear.
I *like* this one :)

try2&3 is the expression you gave with and without the lookahead. Must say I'm lost on the syntax within those brackets ??? Why is '|' sometimes escaped, or is it an 'or' ? Why escape the '[' ?
BTW, I also got similar results with just (?=|)

try4 is one of my first attempts with hideous backtracking.

try5 is a split, even slower than try4.

try6&7 are some split-like regex that also do wastful assignments. Not that I'm proposing we can do without split in Perl of course :)

Hope this is readable, the times are listed at the bottom...

#! /usr/bin/perl -w
use Benchmark;

$data = "|password|+------+|rangers |";

timethese ( 1_000_000,
{
try0 => sub {
$new[0] = $1 if ($data =~ /^\Q|password|+------+|\E ([^|]*) \|$/);
},
try1 => sub {
$new[1] = reverse $1 if ( (reverse $data) =~ /^\| ([^|]*)\|/);
},
try2 => sub {
$new[2] = $1 if ($data =~ /^\|\w+\|\+-+\+\| ([^|]+) \|$/);
},
try3 => sub {
$new[3] = $1 if ($data =~ /^ (?=|[^|]+\+-+\+\|\[^|]+ |$)\|\w+\|\+-+\+\| ([^|]+) \|$/);
},
try4 => sub {
$new[4] = ($data =~ /\| ([^|]*) \|$/ => $1);
},
try5 => sub {
$new[5] = (split '\|', $data, -1)[3];
},
try6 => sub {
$new[6] = $3 if ($data =~ /^\| ([^|]*)\| ([^|]*)\| ([^|]*)\|$/);
},
try7 => sub {
$new[7] = ($data =~ /[^|]+/g)[2];
}
});
{ local $, = ":" ; print "Results ",@new,"\n" }
__END__

The output prints...

Benchmark: timing 1000000 iterations of try0, try1, try2, try3, try4, try5, try6, try7...
try0: 27 secs (26.13 usr 0.00 sys = 26.13 cpu)
try1: 29 secs (28.39 usr 0.01 sys = 28.40 cpu)
try2: 28 secs (28.48 usr 0.00 sys = 28.48 cpu)
try3: 30 secs (29.57 usr 0.00 sys = 29.57 cpu)
try4: 45 secs (45.18 usr 0.00 sys = 45.18 cpu)
try5: 48 secs (47.05 usr 0.00 sys = 47.05 cpu)
try6: 33 secs (33.37 usr 0.00 sys = 33.37 cpu)
try7: 45 secs (44.46 usr 0.00 sys = 44.46 cpu)
Results :rangers:rangers:rangers:rangers:rangers:rangers :rangers :rangers :

Follow-ups:

You are here: irt.org | BBS | Re: Perl string manipulation [This BBS is closed]

©2018 Martin Webb