Hello List,
A posting about splitting a string into n numbered groups of
characters led me to write a short script exploring the ways to do so.
Later I got to wondering how the various approaches compared so added
benchmarking code. Last week I ran the script, made a minor cosmetic
change and ran it again. Noting that the benchmark results changed
markedly I ran it several times like so:
mike@/deb40a:~/perl> { for i in $(seq 1 5);
do echo run $i; ./split_str; done } >> split_str_times.txt
Thinking the number of inerations of the benchmark loop might be
low enough to affect the results I doubled it and reran the above
command.
Condensed here are the results:
2.5M reps
run 1 run 2 run 3 run 4 run 5
via_match via_substr via_arrays via_arrays via_match2
via_unpack via_match via_match via_match via_unpack
via_arrays via_unpack via_match2 via_substr via_arrays
via_match3 via_arrays via_substr via_match3 via_match
via_match2 via_match2 via_match3 via_unpack via_substr
via_substr via_match3 via_unpack via_match2 via_match3
5M reps
run 1 run 2 run 3 run 4 run 5
via_match via_match3 via_match2 via_unpack via_unpack
via_substr via_arrays via_arrays via_match2 via_match3
via_arrays via_match via_match3 via_match3 via_match
via_unpack via_unpack via_substr via_arrays via_substr
via_match3 via_match2 via_unpack via_substr via_arrays
via_match2 via_substr via_match via_match via_match2
I'm at a loss to understand this and hope someone on the list
can explain the variance.
Thanks,
Mike
<CODE>
#!/usr/bin/perl -w
# split_str just another Perl testbed
use strict;
use warnings;
use diagnostics;
{ use Benchmark qw(:all);
my $Testing = ($ARGV[0] || 0);
my $word = "Thequickbrownfox..";
my $size = 3;
if( $Testing )
{ via_arrays($word, $size);
via_substr($word, $size);
via_unpack($word, $size);
via_match( $word, $size);
via_match2($word, $size);
via_match3($word, $size);
}
else
{ print "this is a benchmark, wait ... \n";
cmpthese( 5_000_000, {
'via_arrays' => via_arrays($word, $size),
'via_substr' => via_substr($word, $size),
'via_unpack' => via_unpack($word, $size),
'via_match' => via_match( $word, $size),
'via_match2' => via_match2($word, $size),
'via_match3' => via_match3($word, $size),
});
}
sub via_arrays #
{ my ($word, $size) = @_;
my @array = split //, $word;
my $max = @array - $size;
my @list = ();
for( my $i=0; $i<$max; $i+=$size )
{ push @list, join '', @array[ $i .. $i+$size-1 ];
}
print( 'via_arrays=', map { "$_," } @list, "\n") if($Testing);
}
sub via_substr #
{ my ($word, $size) = @_;
my $max = length( $word ) - $size;
my @list = ();
my $i = 0;
do # substr EXPR,OFFSET,LENGTH
{ push @list, substr( $word, $i, $size );
$i += $size;
} while( $i < $max + $size );
print( 'via_substr=', map { "$_," } @list, "\n") if($Testing);
}
sub via_unpack #
# unpack("x$offset A$length", $what);
{ my ($word, $size) = @_;
my $max = length( $word ) - $size;
my @list = ();
my $i=0;
do
{ push @list, (unpack( "x$i A$size", $word ));
$i += $size;
} while( $i <= $max + $size );
print( 'via_unpack=', map { "$_," } @list, "\n") if($Testing);
}
sub via_match #
{ my ($word, $size) = @_;
my @list = ();
push @list, substr( $word, $-[0], $size )
while $word =~ /.{$size}/g;
print( 'via_match =', map { "$_," } @list, "\n") if($Testing);
}
sub via_match2 #
{ my ($word, $size) = @_;
my @list = ();
push @list, $_ for $word =~ /.{$size}/g;
print( 'via_match2=', map { "$_," } @list, "\n") if($Testing);
}
sub via_match3 #
{ my ($word, $size) = @_;
my @list = $word =~ /.{$size}/g;
print( 'via_match3=', map { "$_," } @list, "\n") if($Testing);
}
}
</CODE>
--
Satisfied user of Linux since 1997.
O< ascii ribbon campaign - stop html mail - www.asciiribbon.org
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/