<nitpick> Before trying to benchmark it, I think I'd try tightening it up a little to eliminate some waste...
my $fil = shift; open (FIL, $fil) || die ("can't read $fil: $!"); my $filstr = do { local $/; <FIL> }; close FIL; my %charhash; $charhash{$_}++ for ( split //, $filstr ); my $ent = entropy(\%charhash, length( $filstr )); printf ("file %s\ncontents entropy = %30.20f\n",$fil,$ent); sub entropy { my ($hashref, $total, $baselog) = @_; $baselog = 0.693147180559945 unless $baselog; # log(2) return undef unless ( ref $hashref and $total > 0 ); my $sum; $sum += $_ * (log($_)/$baselog) for ( map { $_/$total } values %$has +href ); return -$sum; }
It's nice to avoid "use of map in void context", unnecessary copies of data, and "$counter++" where a simpler method (length()) will do. </nitpick>

(updated to eliminate the unnecessary "@values" array; then updated a couple more times to fix details associated with removing @values.)


In reply to Re: file/language entropy calculator by graff
in thread file/language entropy calculator by wufnik

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.