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


in reply to Merge CIDRs

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