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


Groups > comp.lang.python > #92277

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

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.016
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'subject:Python': 0.05; '[0]': 0.07; 'script,': 0.09; 'cc:addr:python-list': 0.10; 'python': 0.11; '1),': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'statements,': 0.16; 'subject:Cython': 0.16; 'time.time()': 0.16; 'wrote:': 0.16; 'element': 0.18; 'cc:2**0': 0.21; 'cc:addr:python.org': 0.21; 'pass': 0.22; 'am,': 0.23; '2015': 0.23; 'import': 0.24; 'seems': 0.24; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'script': 0.25; 'message-id:@mail.gmail.com': 0.28; 'maybe': 0.31; 'print': 0.31; 'seconds': 0.31; 'right?': 0.33; 'received:google.com': 0.34; 'but': 0.36; 'possible': 0.36; 'turn': 0.37; 'subject:: ': 0.37; 'list.': 0.37; 'end': 0.39; 'means': 0.39; 'takes': 0.39; 'your': 0.60; 'please,': 0.63; 'subject:. ': 0.66; 'further,': 0.72; 'chrisa': 0.84; 'random,': 0.84; 'run?': 0.84; 'try,': 0.84; 'to:none': 0.90
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=n1n+EvKt9aJwOHlVReSffbu6gPPMJ4PrtfLQoMlX4vI=; b=OHuqO2oLFbWAh3f8s13RcxRHyxTFm/wgccrY4KKg3rMBi0/qYWtRM7FFeg2joIIGJR IT4hRqDBbACb3+gfzAk5R/Umho2tk83wPdEremw19UY1GX8zXgW9dQeB44EjlWwdyYpJ hxLSBcv4jbw9ev04q6uFL0aN3mFZwkUIiZhBDIyFlM8RNSibs+8igNYXbrKO16QeUB5I nChSpy+X3KkJqzjN9qm9M4RXqSS4uHxpo8qzY9utC1pK0DWqiDBkrBHXnVu14vaPPY26 nTuv+FZaF8+m9IkB96x4P7IwBqBCw7d76QfTRO6j0fNA1mwNCuhbw0buNdkLwTJAiDzB Nhng==
MIME-Version 1.0
X-Received by 10.42.43.199 with SMTP id y7mr19777666ice.12.1433697804186; Sun, 07 Jun 2015 10:23:24 -0700 (PDT)
In-Reply-To <55747C96.7090006@cdreimer.com>
References <55747C96.7090006@cdreimer.com>
Date Mon, 8 Jun 2015 03:23:24 +1000
Subject Re: Python Random vs. Cython C Rand for Dice Rolls
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.264.1433697806.13271.python-list@python.org> (permalink)
Lines 44
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1433697806 news.xs4all.nl 2885 [2001:888:2000:d::a6]:43370
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:92277

Show key headers only | View raw


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.

ChrisA

Back to comp.lang.python | Previous | NextNext 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