use Benchmark qw(cmpthese); sub rbyte { int rand 256 } my @unsorted; for (1..10000) { push @unsorted, join '.', rbyte, rbyte, rbyte, rbyte +} sub sort_by_ip_address { return sort { pack("C4",split(/\./,$a)) cmp pack("C4",split(/\./,$b) +) } @_ } sub sort_faster { return map {$_->[1]} sort {$a->[0] cmp $b->[0]} map {[(pack('C4',spl +it(/\./,$_))), $_]} @_ } my @sorted; cmpthese( 10, { ' slow' => sub { @sorted = sort_by_ip_address(@unsorted) }, 'faster' => sub { @sorted = sort_faster(@unsorted) }, }); __END__ $ perl fastip.pl Benchmark: timing 10 iterations of slow, faster... slow: 80 wallclock secs (40.79 usr + 0.00 sys = 40.79 CPU) @ 0 +.25/s (n=10) faster: 15 wallclock secs ( 6.95 usr + 0.06 sys = 7.01 CPU) @ 1 +.43/s (n=10) s/iter slow faster slow 4.08 -- -83% faster 0.701 482% --

In reply to Re: Re6: Anyone use "xor" in conditionals? by zengargoyle
in thread Anyone use "xor" in conditionals? by bsb

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.