japhy's code is certainly very quick, Benchmarking about
43% better than anything I could come up with on short notice.
The 'radix_sort' is obviously more efficient for this kind of
application than the built in sort of Perl.
However, under Perl 5.6 + strict + '-w', the following change
is required to avoid compilation errors:
sub IPsort {
map inet_ntoa($_),
IP_radix_sort
map inet_aton($_),
@_;
}
The alternative code which Benchmarks in at #2, but has
the advantage of simplicity:
sub byip { inet_ntoa($a) cmp inet_ntoa($b) }
foreach (sort byip @ip_list) ...
BTW, I tested both with 100_000 random IP addresses by 10
runs (approx 86 to 150s per test).
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|