One of the more questionable things my file reading modules does is keep a list of opened file handles in a global hash allowing me to perform multiple operations on a filehandle by name. I was benchmarking the write times for syswrite vs PerlIO and I found something very strange, while keeping the file handle open normally results in being an order of magnitude faster on the test where a
binmode($fh,':encoding(UTF-8)) is used the rate drops from several thousand to 30 runs a second. It doesn't happen on read operations and it doesn't happen with just
binmode($fh) (sans encoding). It's not a huge deal, just validates my decision to make not using PerlIO the default but still, 7700% is a lot of difference for what I would've assumed is the same basic operation. SSSCE and benchmark as follows...
use Benchmark qw/cmpthese timethese/;
my (@array,%handle);
my $file = 'testout.txt';
for (1 .. 1000) { push @array, 'It was the best of times, it was the w
+orst of times...' . $/; }
cmpthese(-8, {
close => sub { &printfile(@array); },
noclose => sub { &printfile(@array,{noclose=>1}); },
});
sub printfile {
my $opts = ( ref $_[-1] eq 'HASH' ) ? pop : {};
my $fh;
my $string = join '', @_;
if ($handle{$file}) { $fh = $handle{$file}; binmode $fh, ':encoding(UT
+F-8)'; }
else {
open($fh, "> :encoding(UTF-8)", $file) || die "Can't open $file fo
+r writing: $!";
$handle{$file} = $fh unless (! $opts->{'noclose'});
}
seek($fh, 0, 0);
print $fh $string;
close $fh unless ($opts->{'noclose'});
}
Benchmark Results
Rate noclose close
noclose 29.9/s -- -99%
close 2330/s 7705% --