Subject: | Performance could be a lot better with small modification |
Date: | Tue, 3 Nov 2015 08:13:34 -0500 |
To: | bug-Bytes-Random [...] rt.cpan.org |
From: | Curt Tilmes <curt [...] tilmes.org> |
This gives exactly the same values, but saves a few cycles in byte
manipulation.
$ perl perftest.pl
Benchmark: timing 10000 iterations of new, old...
new: 8 wallclock secs ( 8.75 usr + 0.00 sys = 8.75 CPU) @
1142.86/s (n=10000)
old: 17 wallclock secs (16.13 usr + 0.00 sys = 16.13 CPU) @
619.96/s (n=10000)
use Bytes::Random qw(random_bytes);
use Benchmark qw(timethese);
timethese(10000, {
'old' => sub { random_bytes(8192) },
'new' => sub { new_random_bytes(8192) },
});
sub new_random_bytes
{
my $number = shift;
return '' unless $number > 0;
pack("C$number", map { int(rand()*256) } 1..$number);
}# end random_bytes()