Code with random module faster on the vm than the vm host...

P

Pascal Bit

Here's the code:

from random import random
from time import clock

s = clock()

for i in (1, 2, 3, 6, 8):
M = 0
N = 10**i

for n in xrange(N):
r = random()
if 0.5 < r < 0.6:
M += 1

k = (N, float(M)/N)

print (clock()-s)

Running on win7 python 2.7 32 bit it uses around 30 seconds avg.
Running on xubuntu, 32 bit, on vmware on windows 7: 20 seconds!
The code runs faster on vm, than the computer itself...
The python version in this case is 1.5 times faster...
I don't understand.

What causes this?
 
A

alex23

from random import random
[...]

Running on win7 python 2.7 32 bit it uses around 30 seconds avg.
Running on xubuntu, 32 bit, on vmware on windows 7: 20 seconds!
The code runs faster on vm, than the computer itself...
The python version in this case is 1.5 times faster...
I don't understand.

What causes this?

The random module uses os.urandom, which relies on OS implementations of
randomness functionality:

"On a UNIX-like system this will query /dev/urandom, and on Windows it
will use CryptGenRandom()."

http://docs.python.org/2/library/os.html#miscellaneous-functions

The linux implementation appears to be faster.
 
N

Ned Batchelder

Here's the code:

from random import random
from time import clock

s = clock()

for i in (1, 2, 3, 6, 8):
M = 0
N = 10**i

for n in xrange(N):
r = random()
if 0.5 < r < 0.6:
M += 1

k = (N, float(M)/N)

print (clock()-s)

Running on win7 python 2.7 32 bit it uses around 30 seconds avg.
Running on xubuntu, 32 bit, on vmware on windows 7: 20 seconds!
The code runs faster on vm, than the computer itself...
The python version in this case is 1.5 times faster...
I don't understand.

What causes this?

The docs for time.clock() make clear that the meaning on Windows and Unix are different:

"On Unix, return the current processor time as a floating point number expressed in seconds."

"On Windows, this function returns wall-clock seconds elapsed since the first call to this function..."

Try the experiment again with time.time() instead.

--Ned.
 
R

Robert Kern

from random import random
[...]

Running on win7 python 2.7 32 bit it uses around 30 seconds avg.
Running on xubuntu, 32 bit, on vmware on windows 7: 20 seconds!
The code runs faster on vm, than the computer itself...
The python version in this case is 1.5 times faster...
I don't understand.

What causes this?

The random module uses os.urandom,

No, it doesn't. random.random() is an alias to the random() method on the
random.Random class, which uses the Mersenne Twister to generate values.
os.urandom() gets called in the initial default seeding, but not for each value.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 
A

alex23

No, it doesn't. random.random() is an alias to the random() method on
the random.Random class, which uses the Mersenne Twister to generate
values. os.urandom() gets called in the initial default seeding, but not
for each value.

That's what I get for rapidly skimming the module rather than looking at
it carefully.

Cheers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top