Dr.Ruud wrote:
>   print substr( $word, $-[0], 3 )
>     while $word =~ /.(?=..)/g;
> 

Doesn't beat substr.

#!/usr/bin/env perl

use strict;
use warnings;

use Data::Dumper;

# Make Data::Dumper pretty
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent   = 1;

# Set maximum depth for Data::Dumper, zero means unlimited
$Data::Dumper::Maxdepth = 0;

use Benchmark qw(:all);

my $word = "thequickbrown";
my $size = 3;

cmpthese( 50_000, {
  'via arrays' => \&via_arrays,
  'via substr' => \&via_substr,
  'via unpack' => \&via_unpack,
  'via match'  => \&via_match,
});

# for testing only
# via_arrays();
# via_substr();
# via_unpack();
# via_match();

sub via_arrays {
  my @array = split //, $word;
  my $max = @array - $size;
  my @list = ();
  for my $i ( 0 .. $max ){
    push @list, join '', @array[ $i .. $i+$size-1 ];
  }
  # print Dumper \...@list;  #for testing only
}

sub via_substr {
  my $max = length( $word ) - $size;
  my @list = ();
  for my $i ( 0 .. $max ){
    push @list, substr( $word, $i, $size );
  }
  # print Dumper \...@list;  #for testing only
}

sub via_unpack {
  my $max = length( $word ) - $size;
  my @list = ();
  for my $i ( 0 .. $max ){
    push @list, (unpack( "A${i}A$size", $word ))[1];
  }
  # print Dumper \...@list;  #for testing only
}

sub via_match {
  my @list = ();
  push @list, substr( $word, $-[0], 3 )
    while $word =~ /.(?=..)/g;
  #print Dumper \...@list;  #for testing only
}


-- 
Just my 0.00000002 million dollars worth,
  Shawn

Programming is as much about organization and communication
as it is about coding.

I like Perl; it's the only language where you can bless your
thingy.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to