While your version took 12 seconds for 16MB on my system:

C:\test>dir 1123355.bin 14/04/2015 16:50 16,777,216 1123355.bin C:\test>1159245 1123355.bin Took 12.897612 secs

(That was the third run so the cache was primed.)

This version took:

C:\test>1159245 1123355.bin Took 3.832763 secs : 3762666 ☺ : 46120 ☻ : 43642 ♥ : 44106 ♦ : 43878 ...

The code;

#! perl -slw use strict; use Time::HiRes qw[ time ]; my $start = time; open I, '<:raw', $ARGV[ 0 ]; my @seen; while( read( I, my $buf, 16384 ) ) { ++$seen[$_] for unpack 'C*', $buf; } printf "Took %f secs\n", time() - $start; printf "%c : %u\n", $_, $seen[$_] for 0 .. 255;

In reply to Re: Count byte/character occurrence (1/4) by BrowserUk
in thread Count byte/character occurrence (quickly) by james28909

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.