http://www.perlmonks.org?node_id=25833

I was recently enjoying(sic) seeing 'script kiddies' trying to scan the ports on my home box. I wrote a script to parse out the packet DENY entries from my messages log file and realized that I should sort the list of IP's to make them easier to read.

Thoughts of complex sorting started to form in my head: split up the quad's and start comparing. I started to look for some elegant ways of doing this, when I realized how much more simple (and potentially more efficient) it would be to pack each IP into it's hex form. "This has to be a much, much faster way", I thought to myself.

So I did a search at Google and found this page written by Uri Guttman and Larry Rosler. They discuss this very thing - giving one example that sorts by breaking up the IP bytes and comparing them, and another example that packs the IP's into hex.

Here are their two examples, slightly paraphrased:

```#naive: O(N*logN)
my @sorted = sort {

my @a = \$a =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/;
my @b = \$b =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/;

\$a <=> \$b ||
\$a <=> \$b ||
\$a <=> \$b ||
\$a <=> \$b

} @unsorted;

#packed: O(N*logN)
my @sorted = sort {

pack('C4' => \$a =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/)
cmp
pack('C4' => \$b =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/)

} @unsorted;
What gets me is the fact that they have the same Big(O), but when benchmarked:
```Benchmark: timing 5000 iterations of naive, packed...
naive: 11 wallclock secs ( 9.91 usr +  0.32 sys = 10.23 CPU)
packed:  8 wallclock secs ( 7.95 usr +  0.08 sys =  8.03 CPU)