Seeing the amount of Perl code involved outside the sort, makes sense. Thanks. Note that sorting is still faster than the original
use strict; use warnings; use Benchmark qw( cmpthese ); for our $n ( map 10**$_, 1, 3, 6 ) { print "Benchmarking $n values\n"; our @n = map int( rand $n ), 1 .. $n; cmpthese -log $n, { orig => q[ my $c=0; for my $elem (@n) { if ($elem > $t){ $c++; } } my $pct= ( @n - $c ) / @n * 100; ], sort => q[ my $t = $n >> 1; @n = sort{ $a<=>$b } @n; my $c=0; $c++ while $n[ $c ] <= $t; my $pct= ( @n - $c ) / @n * 100; ], grep => q[ my $t = $n >> 1; my $pct = grep({ $_ > $t } @n ) / @n * 100; ], }; }
Benchmarking 10 values Rate orig sort grep orig 272566/s -- -11% -41% sort 306129/s 12% -- -34% grep 462968/s 70% 51% -- Benchmarking 1000 values Rate orig grep sort orig 4332/s -- -42% -44% grep 7459/s 72% -- -4% sort 7795/s 80% 5% -- Benchmarking 1000000 values ^C

Lots of variation in the results due to the random input. The first test with orig I ran actually showed sort as being the fastest, although it usually isn't.


In reply to Re^5: Calculated % of data greater than $x in an array by ikegami
in thread Calculated % of data greater than $x in an array by Anonymous Monk

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.