develooper Front page | perl.perl5.porters | Postings from August 2013

[perl #115928] rand() on Windows only uses 15 bits of entropy

Thread Previous | Thread Next
From:
Tony Cook via RT
Date:
August 8, 2013 07:03
Subject:
[perl #115928] rand() on Windows only uses 15 bits of entropy
Message ID:
rt-3.6.HEAD-2552-1375945408-495.115928-15-0@perl.org
On Thu Jul 25 00:00:27 2013, tonyc wrote:
> c) use our drand48() always, don't bother setting $Config{drand01} etc.

I've attached two implementations.

The first is as I said above - a purely private to perl implementation.

But from looking at the perl sources, List-Util uses perl's
seedDrand01() and Drand01(), from from checking CPAN, many CPAN modules
also use them.

So I've prepared an alternate patch that replaces the macros so that
modules will use the new random number implementation.

In terms of implementation - the freebsd implementation is nice and
portable for 32-bit systems, but doesn't take advantage of:

a) native 64-bit systems

b) 64-bit types on 32-bit systems

Each of the patches above includes a 64-bit alternative implementation
of the same algorithm.

My original check used HAS_QUAD, and I was surprised once I ran it on a
32-bit OS (where the compiler has 64-bit integers) that it continued to
use the original portable version.

It turns out that perl.h undefs HAS_QUAD, but there's no explanation of
why (assuming I'm not mis-reading the code.)

Is it the intent that we never uses 64-bit types on 32-bit platforms?

In terms of performance, it's a significant win for an artificial
benchmark, first, on a 32-bit OS, where the compiler has a 64-bit type
(Fedora 18 i386 - but in a VM, AMD Phenom(tm) II X6 1075T Processor):

The original 32-bit implementation:

[tony@p251 perl]$ time ./perl -e 'srand(1); for (1..100000000) { $x +=
rand } print rand;'
0.0607717898801674
real    0m19.099s
user    0m17.565s
sys     0m0.014s
[tony@p251 perl]$ time ./perl -e 'srand(1); for (1..100000000) { $x +=
rand } print rand;'
0.0607717898801674
real    0m18.711s
user    0m16.255s
sys     0m0.003s
[tony@p251 perl]$ time ./perl -e 'srand(1); for (1..100000000) { $x +=
rand } print rand;'
0.0607717898801674
real    0m18.312s
user    0m15.477s
sys     0m0.010s

and the 64-bit implementation:

[tony@p251 perl]$ time ./perl -e 'srand(1); for (1..100000000) { $x +=
rand } print rand;'
0.0607717898801674
real    0m15.707s
user    0m14.188s
sys     0m0.013s
[tony@p251 perl]$ time ./perl -e 'srand(1); for (1..100000000) { $x +=
rand } print rand;'
0.0607717898801674
real    0m13.595s
user    0m10.369s
sys     0m0.007s
[tony@p251 perl]$ time ./perl -e 'srand(1); for (1..100000000) { $x +=
rand } print rand;'
0.0607717898801674
real    0m13.893s
user    0m11.628s
sys     0m0.008s

On a 64-bit OS (Debian wheezy amd64, Intel(R) Core(TM)2 Quad CPU   
Q6600  @ 2.40GHz):

The original 32-bit implementation:

tony@mars:.../git/perl$ time ./perl -e 'srand(1); for (1..100000000) {
$x += rand } print rand;'
0.0607717898801674
real    0m15.280s
user    0m15.177s
sys     0m0.008s
tony@mars:.../git/perl$ time ./perl -e 'srand(1); for (1..100000000) {
$x += rand } print rand;'
0.0607717898801674
real    0m15.406s
user    0m15.393s
sys     0m0.004s
tony@mars:.../git/perl$ time ./perl -e 'srand(1); for (1..100000000) {
$x += rand } print rand;'
0.0607717898801674
real    0m14.985s
user    0m14.977s
sys     0m0.004s

64-bit:

tony@mars:.../git/perl$ time ./perl -e 'srand(1); for (1..100000000) {
$x += rand } print rand;'
0.0607717898801674
real    0m13.411s
user    0m13.397s
sys     0m0.008s
tony@mars:.../git/perl$ time ./perl -e 'srand(1); for (1..100000000) {
$x += rand } print rand;'
0.0607717898801674
real    0m13.494s
user    0m13.425s
sys     0m0.000s
tony@mars:.../git/perl$ time ./perl -e 'srand(1); for (1..100000000) {
$x += rand } print rand;'
0.0607717898801674
real    0m14.086s
user    0m14.073s
sys     0m0.004s

Tony

---
via perlbug:  queue: perl5 status: open
https://rt.perl.org:443/rt3/Ticket/Display.html?id=115928

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About