Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.022 X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'subject:Python': 0.05; 'indexing': 0.07; 'array.': 0.09; 'script,': 0.09; 'cc:addr :python-list': 0.10; 'python': 0.11; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'iteration,': 0.16; 'statements,': 0.16; 'subject:Cython': 0.16; 'threw': 0.16; 'time.time()': 0.16; 'xrange': 0.16; 'wrote:': 0.16; 'element': 0.18; '(in': 0.18; 'purposes': 0.20; 'cc:2**0': 0.21; 'cc:addr:python.org': 0.21; 'together.': 0.22; 'space.': 0.22; 'pass': 0.22; 'am,': 0.23; '2015': 0.23; '(like': 0.23; 'import': 0.24; 'seems': 0.24; 'header:In-Reply-To:1': 0.24; 'mon,': 0.24; 'script': 0.25; 'chris': 0.26; 'error': 0.27; 'message- id:@mail.gmail.com': 0.28; "i'm": 0.29; 'looks': 0.29; 'arrays': 0.29; 'container': 0.29; 'equally': 0.29; 'routine': 0.29; 'way?': 0.29; 'correct': 0.29; 'program,': 0.29; 'print': 0.31; "i'd": 0.31; 'seconds': 0.31; 'fixed': 0.31; 'point': 0.33; 'problem': 0.33; 'null': 0.33; 'particular,': 0.33; 'right?': 0.33; 'received:google.com': 0.34; 'lists': 0.34; 'that,': 0.34; 'wrong': 0.35; 'something': 0.35; 'list': 0.35; 'but': 0.36; 'possible': 0.36; 'faster': 0.36; 'subject:: ': 0.37; 'list.': 0.37; 'difference': 0.38; 'skip:4 10': 0.38; 'rather': 0.38; 'is,': 0.38; 'end': 0.39; 'means': 0.39; 'goes': 0.39; 'expect': 0.39; 'takes': 0.39; 'your': 0.60; 'real': 0.61; 'here.': 0.61; 'side': 0.62; 'taking': 0.62; 'more.': 0.62; 'please,': 0.63; 'between': 0.65; 'here': 0.66; 'subject:. ': 0.66; 'fact,': 0.67; 'further,': 0.72; 'grow': 0.75; 'chrisa': 0.84; 'faster.': 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=tnmrY8pEkesGMzeK3iOroW9NYtN1cljOhZMWDEeKD34=; b=Xdw0wZCZAsJaKHw+X6SnNPOTlJFI7tiVMBm1oG23UL8MMGPybVdYfvawGst0G1C+pY hGjanhfyD7W0WYZLTHjAmyDmJpSbH59AyU0NtslqesOV0CG5ACmvbNlRRFaBsDfo26ew JqY8illQCOqRi0gU3QSFYC1wq/ZYNeQMpvgP4HLZ2T8arqyzTv79F+Ga3c1GuwcmACjc o+6aTbAZ9syKzq3jNwFZGjIkC9J6l9PfIKphYb9b63xJIyDpxQ5oWBcvxEYmoeOdqbS7 luv22Q8OtnjKfqh9gGcs0nOSL32sD/GumyvGawl6QxAck8tVK8VBHaDYUPntnXNCSvlV RO/w== MIME-Version: 1.0 X-Received: by 10.42.154.65 with SMTP id p1mr19907472icw.26.1433700778669; Sun, 07 Jun 2015 11:12:58 -0700 (PDT) In-Reply-To: <55748699.1070009@cdreimer.com> References: <55747C96.7090006@cdreimer.com> <55748699.1070009@cdreimer.com> Date: Mon, 8 Jun 2015 04:12:58 +1000 Subject: Re: Python Random vs. Cython C Rand for Dice Rolls From: Chris Angelico Cc: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 51 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1433700786 news.xs4all.nl 2964 [2001:888:2000:d::a6]:55944 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:92281 On Mon, Jun 8, 2015 at 3:59 AM, C.D. Reimer wrote: > 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? The only thing I was looking at here was range() vs xrange(). Try using xrange here, and see how much faster your null loop is, and then make the corresponding change to your real program and see if it's equally faster. But since your main routine was taking ~200 seconds and the null took ~5, it's not as big a deal as I thought; and a good slab of that time will be spent on the null iteration, rather than the list construction. (In fact, it's entirely possible you have a site.py or something that goes "range = xrange".) > 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? :) The main difference between lists and arrays is that lists can grow and shrink, which you're not using here. So I'd expect that, for the purposes of this program, they're in the same space. You can use a Python list the way you'd use a C array, generally. They can just do ever so much more. ChrisA