in reply to Count byte/character occurrence (quickly)
Don't read the file byte by byte, but read it in larger chunks and have an inner loop counting the characters:
open my $file, '<', shift; binmode($file); my %seen = (); $/ = \( 1024 * 1024 ); # Set default buffer size while ( <$file> ) { for my $buf (split //, $_) { if ( !$seen{$buf} ) { $seen{$buf} = 1; } else { $seen{$buf}++; } } } print "$_ - $seen{$_}\n" for ( keys %seen );
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Count byte/character occurrence (quickly)
by james28909 (Deacon) on Apr 01, 2016 at 06:53 UTC |