XS(XS_main_rand_ranged_value)
{
#ifdef dVAR
dVAR; dXSARGS;
#else
dXSARGS;
#endif
if (items != 2)
croak_xs_usage(cv, "min, max");
{
int min = (int)SvIV(ST(0));
int max = (int)SvIV(ST(1));
int RETVAL;
dXSTARG;
RETVAL = rand_ranged_value(min, max);
XSprePUSH; PUSHi((IV)RETVAL);
}
XSRETURN(1);
}
####
#! perl -slw
use strict;
use Math::Random::MT qw[ rand srand ];
use Benchmark qw[ cmpthese ];
sub aCount {
my $aRef = shift;
my @counts;
++$counts[ $aRef->[ $_ ] ] for 0 .. $#{ $aRef };
return \@counts;
}
sub hCount {
my $aRef = shift;
my %counts;
++$counts{ $aRef->[ $_ ] } for 0 .. $#{ $aRef };
return \%counts;
}
our $N //= 1e6;
our @rands; $#rands = $N;
$rands[ $_ ] = 1+ int( rand( 999 ) ) for 0 .. $N;
cmpthese -1, {
array => q[ my $res = aCount( \@rands ); ],
hash => q[ my $res = hCount( \@rands ); ],
};
__END__
C:\test>junk1
Rate hash array
hash 3.94/s -- -36%
array 6.15/s 56% --
C:\test>junk1
Rate hash array
hash 3.88/s -- -38%
array 6.23/s 61% --
##
##
perl
-nlE"++$h{ $_ } }{ say qq[$_ : $h{ $_ }] for sort{ $a <=> $b } keys %h"
< theBigFileOfInts