File: sortspeed-blocks.t

package info (click to toggle)
libnet-netmask-perl 2.0002-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 320 kB
  • sloc: perl: 2,252; makefile: 2; sh: 1
file content (74 lines) | stat: -rw-r--r-- 1,841 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/perl -w

use strict;

#
# I've been told at times that this or that sort function is
# faster for sorting IP addresses.  I've decied that I won't
# accept undocumented claims anymore.
#
# This file provides a way to test out sort functions.  If you
# think you've got a faster one, please try re-defining &mysortfunc.
# If it's faster, let me know.  If it's not, don't.
#

sub mysortfunc {
    return ( sort @_ );
}

BEGIN {
    unless ( -t STDOUT ) {    ## no critic: (InputOutput::ProhibitInteractiveTest)
        print "1..0 # Skipped: this is for people looking for faster sorts\n";
        exit(0);
    }
}

use Net::Netmask;
use Net::Netmask qw(int2quad quad2int imask);
use Carp;
use Carp qw(verbose);
use Benchmark qw(cmpthese);

sub generate {
    my ($count) = @_;

    my @list;
    while ( $count-- > 0 ) {
        my ( $o1, $o2, $o3, $o4 );
        my $class = int( rand(3) );
        if ( $class == 0 ) {
            ## class A ( 1.0.0.0 - 126.255.255.255 )
            $o1 = int( rand(126) ) + 1;
        } elsif ( $class == 1 ) {
            ## class B ( 128.0.0.0 - 191.255.255.255 )
            $o2 = int( rand(64) ) + 128;
        } else {
            ## class C ( 192.0.0.0 - 223.255.255.255 )
            $o3 = int( rand(32) ) + 192;
        }
        $o2   = int( rand(256) );
        $o3   = int( rand(256) );
        $o4   = int( rand(256) );
        $mask = int( sqrt( rand(1024) ) );
        my $i    = quad2int("$o1.$o2.$o3.$o4") & imask($mask);
        my $base = int2quad($i);
        push( @list, Net::Netmask->new("$base/$mask") );
    }

    return @list;
}

my (@iplist) = generate(5000);

cmpthese(
    -1,
    {
        candidate => sub {
            my (@x) = mysortfunc(@iplist);
        },
        distributed => sub {
            my (@x) = sort_network_blocks(@iplist);
        },
    }
);