Python Random vs. Cython C Rand for Dice Rolls

C.D. Reimer chris at cdreimer.com
Sun Jun 7 13:59:53 EDT 2015


On 6/7/2015 10:23 AM, Chris Angelico wrote:
> Before you go any further, can you just try this script, please, and
> see how long it takes to run?
>
> import random, time
> startTime = time.time()
> for i in range(50000000):
>      pass
> print '\n', time.time() - startTime
>
> I know, seems a stupid thing to try, right? But you're using Python 2,
> as evidenced by the print statements, and that means that range() is
> constructing a 50M element list. It's entirely possible that that's a
> significant part of your time - allocating all that memory, populating
> it, and then disposing of it at the end (maybe).

PS Z:\projects\programming\python\basic_games\fastdice> python test_random_time.py

4.84700012207


So... I'm not looking at the problem on the Python side in the correct way?

In particular, I'm using the wrong container type?

I was thinking "array" (like the Cython version) when I put the Python 
script together. I got an error message at one point about indexing the 
"list" that threw me off for a while. Since it looks like an array, 
walks like an array, and quack likes array, I fixed the indexing problem 
like an array. A list != array? :)

Chris R.



More information about the Python-list mailing list