#!/usr/bin/perl -w
use Benchmark;
use strict;
my $count = shift || -3 ; # set the test counter
my @array;
#load up an array of crap
push @array, rand(256) for 1 .. 1000;
open NULL, ">/dev/null" or die $!;
timethese (
$count,
{
'join' => \&print_join,
'map' => \&print_map,
'list map' => \&print_map_list,
'field sep' => \&print_field,
'for' => \&print_for
}
);
# use join to print each array elemnet.
sub print_join {
print NULL join("\n", @array);
}
# use map to print each array elemnt,
sub print_map {
print NULL map{ $_ .= "\n"} @array;
}
# same but a no concat
sub print_map_list {
print NULL map { ($_, "\n") } @array;
}
# use map to print each array elemnt,
sub print_field {
local $, = "\n";
print NULL @array;
}
# use a loop.
sub print_for {
print NULL $_, "\n" for @array;
}
Yields:
Benchmark: running field sep, for, join, list map, map, each for at least 9 CPU seconds...
field sep: 11 wallclock secs ( 9.60 usr + 0.05 sys = 9.65 CPU) @ 1121.04/s (n=10818)
for: 11 wallclock secs ( 9.28 usr + 0.02 sys = 9.30 CPU) @ 621.40/s (n=5779)
join: 11 wallclock secs ( 9.20 usr + 0.02 sys = 9.22 CPU) @ 2888.07/s (n=26628)
list map: 11 wallclock secs ( 9.07 usr + 0.04 sys = 9.11 CPU) @ 268.94/s (n=2450)
map: 32 wallclock secs (26.02 usr + 0.26 sys = 26.28 CPU) @ 57.95/s (n=1523)
What happened with your field sep test? I can't get it as fast as you claim it to be. You should also note that join does not put a newline after the final item.
- Yes, I reinvent wheels.
- Spam: Visit eurotraQ.
|