Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!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.025 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'subject:Python': 0.05; 'python': 0.09; 'bytearray': 0.09; 'btw': 0.16; 'natively,': 0.16; 'offsets': 0.16; 'prog': 0.16; 'stringent': 0.16; 'wrote:': 0.17; 'thu,': 0.17; '>>>': 0.18; 'feb': 0.19; 'import': 0.21; 'header :In-Reply-To:1': 0.25; 'looks': 0.26; 'skip:[ 10': 0.26; 'am,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'chris': 0.28; 'piece': 0.29; 'though.': 0.29; 'array': 0.29; "aren't": 0.33; 'to:addr :python-list': 0.33; 'requirements': 0.33; 'that,': 0.34; 'skip:b 20': 0.34; 'received:google.com': 0.34; 'list': 0.35; 'faster': 0.35; 'said,': 0.35; 'received:209.85.220': 0.35; 'received:209.85': 0.35; 'tool': 0.36; "didn't": 0.36; 'skip:p 20': 0.36; 'subject: (': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'more': 0.63; 'subject:First': 0.65; 'square': 0.75; '19%': 0.84; '2013': 0.84; 'to:name:python': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:content-type; bh=X+qFCntsXUWKbMkMBEJQmzoaUUNjIL3L4XvC4VUqvpk=; b=V48JcHWd1dt9/UFJGkiNhaEs87v6Hfq3lz1TP+yMQc2ZfqrJqPsfNRXwNNym22AWMX jyl+/xDtRbBgLftjcGaVG5xiLYgEMX9H9/GBtcGulKy8hX8GUjnkpIu0ga/k3/2K53Ai PO2JPDdasuUORxJM9uJRpcHrkWhJmVRMGtoitOFds0C7/Pv4Frifd6hkxGQuSZRE9E/R UFInt2QnYRrYdAOaNs7/8Wr1RVAltsXOh0vHJ57NO0d3m18XDfS9HP0PTgOteF7bOm50 bggPhGwHXVFp23DKcCQOPDCvp65wNymMYujY8KWGLap/+PGc1kMx3i1dvdFzCCEYK0xW FlJg== X-Received: by 10.68.50.101 with SMTP id b5mr6425986pbo.106.1360866695756; Thu, 14 Feb 2013 10:31:35 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <2013021417481038756-chrishinsley@gmailcom> References: <2013021323250974803-chrishinsley@gmailcom> <5evoh8p6a9sqsdq1b98ahs3ki6s4d70f9l@4ax.com> <2013021417481038756-chrishinsley@gmailcom> From: Ian Kelly Date: Thu, 14 Feb 2013 11:30:55 -0700 Subject: Re: First attempt at a Python prog (Chess) To: Python Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 27 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1360866699 news.xs4all.nl 6851 [2001:888:2000:d::a6]:54547 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:38875 On Thu, Feb 14, 2013 at 10:48 AM, Chris Hinsley wrote: > Is a Python list as fast as a bytearray ? I didn't copy a C prog BTW ! >>> from timeit import Timer >>> t1 = Timer("board[36] = board[20]; board[20] = ' '", "board = bytearray('RNBQKBNRPPPPPPPP pppppppprnbqkbnr')") >>> min(t1.repeat(10)) 0.1678651895701826 >>> t2 = Timer("board[3][4] = board[1][4]; board[1][4] = ' '", "board = [list('RNBQKBNR'), ['P'] * 8] + [[' ']*8 for i in range(4)] + [['p'] * 8, list('rnbqkbnr')]") >>> min(t2.repeat(10)) 0.2080088391122672 Natively, it looks like the bytearray is about 19% faster for moving a piece from one square to another. Those array offsets aren't calculated for free, though. Look what happens when we have to do the math: >>> t3 = Timer("board[r1*8+c1] = board[r2*8+c2]; board[r2*8+c2] = ' '", "board = bytearray('RNBQKBNRPPPPPPPP pppppppprnbqkbnr'); r1 = 3; r2 = 1; c1 = c2 = 4") >>> min(t3.repeat(10)) 0.314191887516472 >>> t4 = Timer("board[r1][c1] = board[r2][c2]; board[r2][c2] = ' '", "board = [list('RNBQKBNR'), ['P'] * 8] + [[' ']*8 for i in range(4)] + [['p'] * 8, list('rnbqkbnr')]; r1 = 3; r2 = 1; c1 = c2 = 4") >>> min(t4.repeat(10)) 0.24427881197186707 That said, the philosophy of Python focuses more on readability than on speed. The goalpost for speed in a Python program is that it be "fast enough". If you have more stringent speed requirements than that, then Python may not be the right tool for the job.