Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

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 ( [id://11162874]=note: print w/replies, xml ) Need Help??


in reply to Why is using binmode on a file handle 77 times slower than just closing it and re-opening?

The problem is binmode doesn't change the layer of the filehandle, it adds a new layer.

Try appending the following line to your test:

warn PerlIO::get_layers($handle{$file});
And then try to count how many times the string encoding(utf-8-strict)utf8repeats. In my case, it was 922 ×
my @l = grep /utf-?8/, PerlIO::get_layers($handle{$file}); warn @l / 2;

To fix that, clear the layers before applying new ones by starting with :raw:

binmode $fh, ':raw:encoding(UTF-8)';
The result becomes agreeable:
Rate close noclose close 2137/s -- -28% noclose 2951/s 38% --
And the warning now shows a pretty low number.

Updated: Show how to count the repeated layers.

Update 2: On MSWin, you might need to add :crlf after :raw if needed, too.

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
  • Comment on Re: Why is using binmode on a file handle 77 times slower than just closing it and re-opening?
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: Why is using binmode on a file handle 77 times slower than just closing it and re-opening?
by Maelstrom (Beadle) on Nov 25, 2024 at 09:00 UTC
    Much thanks the universe makes sense again. The results got even better when I remembered to add the seek statement.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11162874]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2025-06-16 03:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.