use Benchmark;
use Symbol;
my $fh = gensym;
open $fh, ">/dev/null" or die;
my @text = (
"<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML//EN\">\n",
"<HTML>\n",
" <HEAD>\n",
" <TITLE>\n",
" Test page\n",
" </TITLE>\n",
" </HEAD>\n",
" <BODY BGCOLOR=\"black\" TEXT=\"white\">\n",
" <H1>\n",
" Test page \n",
" </H1>\n",
" <A HREF=\"foo.html\">foo</A>\n",
"text line that emulates some real output\n" x 100,
" <HR>\n",
" </BODY>\n",
"</HTML>\n",
);
my $text = join "", @text;
sub multi {
my @copy = @text;
my_print($_) for @copy;
}
sub single {
my $copy = $text;
my_print($copy);
}
sub array {
my @copy = @text;
my_print(@copy);
}
sub ref_arr {
my @refs = \(@text);
my_print(@refs);
}
sub concat {
my $buffer;
$buffer .= $_ for @text;
my_print($buffer);
}
sub my_join {
my $buffer = join '', @text;
my_print($buffer);
}
sub my_print {
for (@_) {
print $fh ref($_) ? $$_ : $_;
}
}
timethese(100_000, {
join => \&my_join,
array => \&array,
ref_arr => \&ref_arr,
multi => \&multi,
single => \&single,
concat => \&concat,
});
timethese(100_000, {
'array /b' => sub {my $ofh=select($fh);$|=0;select($ofh); array( ) },
'array /u' => sub {my $ofh=select($fh);$|=1;select($ofh); array( ) },
'ref_arr/b' => sub {my $ofh=select($fh);$|=0;select($ofh); ref_arr( )},
'ref_arr/u' => sub {my $ofh=select($fh);$|=1;select($ofh); ref_arr( )},
'multi /b' => sub {my $ofh=select($fh);$|=0;select($ofh); multi( ) },
'multi /u' => sub {my $ofh=select($fh);$|=1;select($ofh); multi( ) },
'single /b' => sub {my $ofh=select($fh);$|=0;select($ofh); single( ) },
'single /u' => sub {my $ofh=select($fh);$|=1;select($ofh); single( ) },
'concat /b' => sub {my $ofh=select($fh);$|=0;select($ofh); concat( ) },
'concat /u' => sub {my $ofh=select($fh);$|=1;select($ofh); concat( ) },
'join /b' => sub {my $ofh=select($fh);$|=0;select($ofh); my_join( )},
'join /u' => sub {my $ofh=select($fh);$|=1;select($ofh); my_join( )},
});