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


Groups > comp.lang.python > #92298

Re: Python Random vs. Cython C Rand for Dice Rolls

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!newsfeed.pionier.net.pl!feed.xsnews.nl!border03.ams.xsnews.nl!feeder03.ams.xsnews.nl!abp001.ams.xsnews.nl!frontend-F10-09.ams.news.kpn.nl
From Cecil Westerhof <Cecil@decebal.nl>
Newsgroups comp.lang.python
Subject Re: Python Random vs. Cython C Rand for Dice Rolls
Organization Decebal Computing
References <55747C96.7090006@cdreimer.com> <mailman.264.1433697806.13271.python-list@python.org>
X-Face "(y8cC@tg_12{">GF'UXTW]FHI2wMiZNrnf'1EFQ&O#$m:f#O7+7}kR<J%a^F2lh4[N~Yz4 nSp#c+aQo1b5=?HcNEkQ7QzF<])O3X4MDL/AYjys&*mt>,v+Pti8=Vi/Z"g^?b"E
X-Homepage http://www.decebal.nl/
Date Sun, 07 Jun 2015 21:34:03 +0200
Message-ID <87d217nwic.fsf@Equus.decebal.nl> (permalink)
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)
Cancel-Lock sha1:hXvY8aWxzSaOqBCz1haTbj2IyXs=
MIME-Version 1.0
Content-Type text/plain
Lines 53
NNTP-Posting-Host 81.207.62.244
X-Trace 1433706352 news.kpn.nl 20910 81.207.62.244@kpn/81.207.62.244:55468
Xref csiph.com comp.lang.python:92298

Show key headers only | View raw


On Sunday  7 Jun 2015 19:23 CEST, Chris Angelico wrote:

> On Mon, Jun 8, 2015 at 3:17 AM, C.D. Reimer <chris@cdreimer.com> wrote:
>> This is the Python script that takes ~197 seconds to complete.
>>
>> import random, time
>>
>> startTime = time.time()
>>
>> f = [0] * 12
>>
>> for i in range(50000000):
>>
>> a = random.randint(1,6)
>>
>> b = random.randint(1,6)
>>
>> f[(a + b) - 1] += 1
>>
>> print "\nTOTAL SPOTS","\tNUMBER OF TIMES\n"
>>
>> for i in range(1,12):
>>
>> print ' ' + str(i + 1), '\t\t ', f[i]
>>
>> print '\n', time.time() - startTime
>
> 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).
>
> Maybe it'll turn out not to be significant, but there's only one way
> to find out.

I tried this in 2 and 3. In 3 it takes 3.13 seconds and 2 7.5 seconds.
A significant difference, but not in the whole of what he was trying
to do I think.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Re: Python Random vs. Cython C Rand for Dice Rolls Chris Angelico <rosuav@gmail.com> - 2015-06-08 03:23 +1000
  Re: Python Random vs. Cython C Rand for Dice Rolls Cecil Westerhof <Cecil@decebal.nl> - 2015-06-07 21:34 +0200

csiph-web