Net-IPAddress-Util

 view release on metacpan or  search on metacpan

t/20-time-trials.t  view on Meta::CPAN

#!/usr/bin/env perl

BEGIN {
  if ($] ge '5.012') {
    use strict;
    use warnings;
  }
  if ($] ge '5.026') {
    use lib '.';
  }
}

use Net::IPAddress::Util qw( :constr radix_sort );
use Net::IPAddress::Util::Range;
use Net::IPAddress::Util::Collection;
use Test::More tests => 1;
use Time::HiRes qw(time);

SKIP: {
  skip 'Author tests only', 1 unless -d '.git';
  skip 'Set $ENV{IP_UTIL_TIME_TRIALS} if you want time trials', 1 unless $ENV{IP_UTIL_TIME_TRIALS};
  # TODO better method for finding the break-even point. Newton's?
  diag('This is going to take a while. Unset $ENV{IP_UTIL_TIME_TRIALS} if you don\'t want time trials.');
  my ($savings, $count, $total);
  my $mult = $ENV{IP_UTIL_TIME_TRIALS};
  my @to_sort;
  my ($r, $tr, $p, $tp);
  my (@rsorted, @psorted);
  {
    $total = 0;
    $savings = 0;
    for my $iter (1 .. $mult) {
      $count = 100;
      while ($count-->0) {
        $total += $count;
        @to_sort = ();
        for my $i (1 .. $count) {
          push @to_sort, mk_v4();
        }
        {
          $r = time;
          @rsorted = radix_sort(@to_sort);
          $tr = time - $r;
        }
        {
          $p = time;
          @psorted = sort { $a <=> $b } @to_sort;
          $tp = time - $p;
        }
        $savings += eval { ($tp - $tr) / $tp };
      }
    }
    $savings = sprintf('%.3f', (0 - $savings) / $mult);
    diag("Radix-Sorting (1 .. 100) IPv4 addresses averages $savings\% faster");
  }
  {
    $total = 0;
    $savings = 0;
    for my $iter (1 .. $mult) {
      $count = 100;
      while ($count-->0) {
        $total += $count;
        @to_sort = ();
        for my $i (1 .. $count) {
          push @to_sort, mk_v6();
        }
        {
          $r = time;



( run in 1.294 second using v1.01-cache-2.11-cpan-a49fcb8fa48 )