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
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.