You should try Sort::Key::IPv4 or even Sort::Key::Radix.

The modified benchmarking script:

#!/usr/bin/perl use strict; use warnings; use Benchmark qw(cmpthese); use Socket qw(inet_aton inet_ntoa); use Sort::Key::IPv4 qw(ipv4sort ipv4_to_uv); use Sort::Key::Radix qw(ukeysort); chomp( my @ips=(<>) ); cmpthese(-10, { inet_grt => sub { my @sorted = map { inet_ntoa $_ } sort { $a cmp $b } map { inet_aton $_ } @ips; }, inet_grt2 => sub { my @sorted = map { unpack 'x4A*' } sort { $a cmp $b } map { pack('A4A*', inet_aton($_), $_) } @ips; }, inet_orc => sub { my @sorted = sort by_ip @ips; }, splitwise => sub { my @sorted = map $_->[0], sort { $a->[1] <=> $b->[1] or $a->[2] <=> $b->[2] or $a->[3] <=> $b->[3] or $a->[4] <=> $b->[4] } map [$_, split /\./], @ips; }, split_int => sub { my @sorted = map { join '.', unpack("CCCC", pack("N",$_)) +} sort { $a <=> $b } map { unpack("N",pack("CCCC",split /\./)) } @ips; }, naive => sub { my @sorted = sort { my @a = split /\./, $a; my @b = split /\./, $a; $a[0] <=> $b[0] or $a[1] <=> $b[1] or $a[2] <=> $b[2] or $a[3] <=> $b[3] } @ips; }, sk => sub { my @sorted = ipv4sort @ips; }, skr => sub { my @sorted = &ukeysort(\&ipv4_to_uv, @ips); } }); { my %cache; sub by_ip { my $orc_a = ($cache{$a} ||= inet_aton($a)); my $orc_b = ($cache{$b} ||= inet_aton($b)); $orc_a cmp $orc_b; } }

And that's what I get on my machine:

Rate splitwise inet_orc split_int naive inet_grt2 inet_grt + sk skr splitwise 94.7/s -- -19% -36% -42% -68% -68% + -88% -89% inet_orc 117/s 24% -- -20% -28% -60% -61% + -85% -87% split_int 147/s 55% 26% -- -9% -50% -50% + -82% -83% naive 163/s 72% 39% 10% -- -45% -45% + -80% -81% inet_grt2 295/s 211% 152% 100% 81% -- -1% + -63% -66% inet_grt 297/s 214% 154% 102% 83% 1% -- + -63% -66% sk 804/s 749% 587% 446% 395% 173% 170% + -- -7% skr 868/s 816% 642% 490% 434% 194% 192% + 8% --

In reply to Re: RFC: Sorting IPv4 addresses by salva
in thread RFC: Sorting IPv4 addresses by bv

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.