processing strings char-by-char

Discussion in 'Perl Misc' started by Rainer Weikusat, Aug 18, 2013.

  1. 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;
    }});
    Rainer Weikusat, Aug 18, 2013
    #1
    1. Advertising

  2. On 8/18/2013 12:10 PM, Rainer Weikusat wrote:
    > 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;

    --
    Charles DeRykus
    Charles DeRykus, Aug 18, 2013
    #2
    1. Advertising

  3. Charles DeRykus <> writes:

    [...]

    >> $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);
    Rainer Weikusat, Aug 19, 2013
    #3
  4. Rainer Weikusat wrote:
    > Charles DeRykus<> writes:
    >
    > [...]
    >
    >>> $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
    --
    Any intelligent fool can make things bigger and
    more complex... It takes a touch of genius -
    and a lot of courage to move in the opposite
    direction. -- Albert Einstein
    John W. Krahn, Aug 20, 2013
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pmatos
    Replies:
    3
    Views:
    385
    Andrew Koenig
    Mar 7, 2005
  2. John Devereux

    Should I use "char" or "unsigned char" for strings?

    John Devereux, Mar 28, 2005, in forum: C Programming
    Replies:
    4
    Views:
    628
    Eric Sosman
    Mar 29, 2005
  3. lovecreatesbeauty
    Replies:
    1
    Views:
    1,007
    Ian Collins
    May 9, 2006
  4. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    734
    Malcolm
    Jun 24, 2006
  5. Rainer Weikusat

    processing strings char-by-char

    Rainer Weikusat, Aug 18, 2013, in forum: Perl Misc
    Replies:
    0
    Views:
    143
    Rainer Weikusat
    Aug 18, 2013
Loading...

Share This Page