[Tutor] Why is random.choice so much slower than random.random()?

Kent Johnson kent37 at tds.net
Thu Oct 12 15:15:17 CEST 2006


Dick Moores wrote:
> At 04:33 AM 10/12/2006, Kent Johnson wrote:
>> Looking at the code and your numbers, my guess is that the time to make
>> a Python function call on your computer is about 2 microseconds (20 msec
>> / 10000). (I mean the entire overhead - looking up the function and
>> actually calling it.)
> 
> Hm. Does this test include the entire overhead?
> 
> C:\>python -m timeit  -r 3 -s"from random import random" "random()"
> 10000000 loops, best of 3: 0.157 usec per loop

I don't know why that one is faster. Maybe because it is calling a 
function in C, or because there are no arguments? Someone else will have 
to answer that.

Kent



More information about the Tutor mailing list