Maelstrom has asked for the wisdom of the Perl Monks concerning the following question:
Benchmark Resultsuse 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'}); }
Rate noclose close noclose 29.9/s -- -99% close 2330/s 7705% --
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Why is using binmode on a file handle 77 times slower than just closing it and re-opening?
by choroba (Cardinal) on Nov 25, 2024 at 08:41 UTC | |
by Maelstrom (Beadle) on Nov 25, 2024 at 09:00 UTC |