processing strings char-by-char

R

Rainer Weikusat

So far, I came up with three obvious ways to do that:

$r ^= ord($1) while $string =~ /\G(.)/g;

This extracts successive characters from the string with a 'moving
regex match'.

$r ^= ord($_) for split(//, $string);

This splits the string into a list of characters and loops over that.

$r ^= ord(substr($string, $_, 1)) for 0 .. length($string) - 1;

This loops over the 'character positions' in $string and uses substr
to extract the corresponding characters.

When benchmarking these three, the one I like best (the regex match)
comes out slowest. The 'split string, loop over list' method is about
twice as fast as the regex and the most clumsy one (IMHO), substr,
runs at about 1.5 times the speed of the split loop.

-----------------
use Benchmark;

my $string = 'A' x 100;

timethese(-3,
{
while => sub {
my $r;
$r ^= ord($1) while $string =~ /\G(.)/g;
},

for => sub {
my $r;
$r ^= ord($_) for split(//, $string);
},

substr => sub {
my $r;
$r ^= ord(substr($string, $_, 1)) for 0 .. length($string) - 1;
}});
 
C

Charles DeRykus

So far, I came up with three obvious ways to do that:

$r ^= ord($1) while $string =~ /\G(.)/g;

This extracts successive characters from the string with a 'moving
regex match'.

$r ^= ord($_) for split(//, $string);

This splits the string into a list of characters and loops over that.

$r ^= ord(substr($string, $_, 1)) for 0 .. length($string) - 1;

This loops over the 'character positions' in $string and uses substr
to extract the corresponding characters.

When benchmarking these three, the one I like best (the regex match)
comes out slowest. The 'split string, loop over list' method is about
twice as fast as the regex and the most clumsy one (IMHO), substr,
runs at about 1.5 times the speed of the split loop.

-----------------
use Benchmark;

my $string = 'A' x 100;

timethese(-3,
{
while => sub {
my $r;
$r ^= ord($1) while $string =~ /\G(.)/g;
},

for => sub {
my $r;
$r ^= ord($_) for split(//, $string);
},

substr => sub {
my $r;
$r ^= ord(substr($string, $_, 1)) for 0 .. length($string) - 1;
}});

Not fast... "aber an der unterseite vom Fass"...

my $r;
$r ^= unpack "x${_}a",$string for 0..length($string)-1;
 
R

Rainer Weikusat

[...]
$r ^= ord($1) while $string =~ /\G(.)/g;
[...]
$r ^= ord($_) for split(//, $string);
[...]

$r ^= ord(substr($string, $_, 1)) for 0 .. length($string) - 1;
[...]

$r ^= unpack "x${_}a",$string for 0..length($string)-1;

I had the idea that something should be possible here with 'pack' for
some weird reason but didn't think of unpack. Another midfield method
(slower than split, faster than the slower ones)

$r ^= ord($_) for unpack('(a)*', $string);
 
J

John W. Krahn

Rainer said:
[...]
$r ^= ord($1) while $string =~ /\G(.)/g;
[...]
$r ^= ord($_) for split(//, $string);
[...]

$r ^= ord(substr($string, $_, 1)) for 0 .. length($string) - 1;
[...]

$r ^= unpack "x${_}a",$string for 0..length($string)-1;

I had the idea that something should be possible here with 'pack' for
some weird reason but didn't think of unpack. Another midfield method
(slower than split, faster than the slower ones)

$r ^= ord($_) for unpack('(a)*', $string);


$r ^= ord($_) for $string =~ /./g;



John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top