Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #58849 > unrolled thread

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

Started byPascal Bit <pascalbit@hotmail.com>
First post2013-11-08 18:48 +0100
Last post2013-11-11 01:31 +0000
Articles 6 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  Code with random module faster on the vm than the vm host... Pascal Bit <pascalbit@hotmail.com> - 2013-11-08 18:48 +0100
    Re: Code with random module faster on the vm than the vm host... alex23 <wuwei23@gmail.com> - 2013-11-11 10:49 +1000
      Re: Code with random module faster on the vm than the vm host... Robert Kern <robert.kern@gmail.com> - 2013-11-11 01:19 +0000
        Re: Code with random module faster on the vm than the vm host... alex23 <wuwei23@gmail.com> - 2013-11-11 12:16 +1000
    Re: Code with random module faster on the vm than the vm host... Ned Batchelder <ned@nedbatchelder.com> - 2013-11-10 17:15 -0800
      Re: Code with random module faster on the vm than the vm host... Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-11-11 01:31 +0000

#58849 — Code with random module faster on the vm than the vm host...

FromPascal Bit <pascalbit@hotmail.com>
Date2013-11-08 18:48 +0100
SubjectCode with random module faster on the vm than the vm host...
Message-ID<mailman.2249.1383942084.18130.python-list@python.org>
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?

[toc] | [next] | [standalone]


#59038

Fromalex23 <wuwei23@gmail.com>
Date2013-11-11 10:49 +1000
Message-ID<l5p9j4$ad6$1@dont-email.me>
In reply to#58849
On 9/11/2013 3:48 AM, Pascal Bit wrote:
> 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.

[toc] | [prev] | [next] | [standalone]


#59040

FromRobert Kern <robert.kern@gmail.com>
Date2013-11-11 01:19 +0000
Message-ID<mailman.2357.1384132761.18130.python-list@python.org>
In reply to#59038
On 2013-11-11 00:49, alex23 wrote:
> On 9/11/2013 3:48 AM, Pascal Bit wrote:
>> 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

[toc] | [prev] | [next] | [standalone]


#59044

Fromalex23 <wuwei23@gmail.com>
Date2013-11-11 12:16 +1000
Message-ID<l5pela$dk$1@dont-email.me>
In reply to#59040
On 11/11/2013 11:19 AM, Robert Kern wrote:
> On 2013-11-11 00:49, alex23 wrote:
>> 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.

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

Cheers.

[toc] | [prev] | [next] | [standalone]


#59039

FromNed Batchelder <ned@nedbatchelder.com>
Date2013-11-10 17:15 -0800
Message-ID<c85d4207-0775-461b-a411-7bc3c08d913a@googlegroups.com>
In reply to#58849
On Friday, November 8, 2013 12:48:04 PM UTC-5, Pascal Bit wrote:
> 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.

[toc] | [prev] | [next] | [standalone]


#59041

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2013-11-11 01:31 +0000
Message-ID<mailman.2358.1384133501.18130.python-list@python.org>
In reply to#59039
On 11/11/2013 01:15, Ned Batchelder wrote:
> On Friday, November 8, 2013 12:48:04 PM UTC-5, Pascal Bit wrote:
>> 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.
>

http://www.python.org/dev/peps/pep-0418/ for some related reading about 
Python time functions.

-- 
Python is the second best programming language in the world.
But the best has yet to be invented.  Christian Tismer

Mark Lawrence

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web