I think your original suggestion, of using
Net::CIDR::cidr2range
and merging the ranges end-to-end, was better.
Your new program seems to be slower than the one I wrote
following your
Net::CIDR
suggestion. Here's my program for comparison:
#!/usr/bin/perl
use Net::CIDR 'cidr2range', 'range2cidr';
my @ranges;
my ($cur_start, $cur_end, $cs, $ce);
while (<>) {
chomp;
my @r = cidr2range($_);
my ($r_start, $r_end) = split /-/, $r[0];
my ($rs, $re) = map {inet_to_n($_)} $r_start, $r_end;
if (! defined $cur_start) {
($cur_start, $cur_end) = ($r_start, $r_end);
($cs, $ce) = ($rs, $re);
} else {
if ($rs == $ce + 1) {
$cur_end = $r_end;
$ce = $re;
} else {
print join "\n", range2cidr("$cur_start-$cur_end"), "";
($cur_start, $cur_end) = ($r_start, $r_end);
($cs, $ce) = ($rs, $re);
}
}
} continue {
print STDERR "$. records processed\n" if $. % 1000 == 0;
}
print join "\n", range2cidr("$cur_start-$cur_end"), ""
if defined $cur_start;
sub inet_to_n {
unpack "N", pack "C4", split /\./, shift();
}
I ran your program on
nm5.in and waited three minutes.
Then I started my program. My program finished first.
Of course, I might have made a mistake in the benchmarking somewhere.
--
Mark Dominus
Perl Paraphernalia
-
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.