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


Groups > comp.lang.python > #38875

Re: First attempt at a Python prog (Chess)

References <2013021323250974803-chrishinsley@gmailcom> <5evoh8p6a9sqsdq1b98ahs3ki6s4d70f9l@4ax.com> <2013021417481038756-chrishinsley@gmailcom>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2013-02-14 11:30 -0700
Subject Re: First attempt at a Python prog (Chess)
Newsgroups comp.lang.python
Message-ID <mailman.1776.1360866699.2939.python-list@python.org> (permalink)

Show all headers | View raw


On Thu, Feb 14, 2013 at 10:48 AM, Chris Hinsley <chris.hinsley@gmail.com> 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.

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


Thread

First attempt at a Python prog (Chess) Chris Hinsley <chris.hinsley@gmail.com> - 2013-02-13 23:25 +0000
  Re: First attempt at a Python prog (Chess) Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-02-13 23:55 +0000
    Re: First attempt at a Python prog (Chess) Chris Hinsley <chris.hinsley@gmail.com> - 2013-02-14 01:31 +0000
  Re: First attempt at a Python prog (Chess) Tim Roberts <timr@probo.com> - 2013-02-13 22:05 -0800
    Re: First attempt at a Python prog (Chess) Chris Hinsley <chris.hinsley@gmail.com> - 2013-02-14 17:48 +0000
      Re: First attempt at a Python prog (Chess) Ian Kelly <ian.g.kelly@gmail.com> - 2013-02-14 11:30 -0700
      Re: First attempt at a Python prog (Chess) Tim Roberts <timr@probo.com> - 2013-02-18 20:15 -0800
        Re: First attempt at a Python prog (Chess) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-19 06:24 +0000
        Re: First attempt at a Python prog (Chess) Ian Kelly <ian.g.kelly@gmail.com> - 2013-02-19 12:41 -0700
  Re: First attempt at a Python prog (Chess) jkn <jkn_gg@nicorp.f9.co.uk> - 2013-02-14 13:14 -0800
    Re: First attempt at a Python prog (Chess) Chris Hinsley <chris.hinsley@gmail.com> - 2013-02-14 22:13 +0000
      Re: First attempt at a Python prog (Chess) Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-02-14 22:09 -0500
  Re: First attempt at a Python prog (Chess) Rick Johnson <rantingrickjohnson@gmail.com> - 2013-02-14 21:05 -0800
  Re: First attempt at a Python prog (Chess) Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-02-15 11:22 +0000
    Re: First attempt at a Python prog (Chess) Neil Cerutti <neilc@norwich.edu> - 2013-02-15 16:17 +0000
      Re: First attempt at a Python prog (Chess) MRAB <python@mrabarnett.plus.com> - 2013-02-15 17:52 +0000
        Re: First attempt at a Python prog (Chess) Neil Cerutti <neilc@norwich.edu> - 2013-02-19 21:10 +0000
      Re: First attempt at a Python prog (Chess) Matt Jones <matt.walker.jones@gmail.com> - 2013-02-15 12:03 -0600
  Re: First attempt at a Python prog (Chess) Tim Golden <mail@timgolden.me.uk> - 2013-02-15 11:36 +0000
  Re: First attempt at a Python prog (Chess) Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-02-15 13:11 +0000
  Re: First attempt at a Python prog (Chess) Tim Golden <mail@timgolden.me.uk> - 2013-02-15 15:36 +0000
    Re: First attempt at a Python prog (Chess) Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-02-15 18:48 +0200
  Re: First attempt at a Python prog (Chess) Chris Angelico <rosuav@gmail.com> - 2013-02-16 02:49 +1100
  Re: First attempt at a Python prog (Chess) Tim Golden <mail@timgolden.me.uk> - 2013-02-15 17:37 +0000
  Re: First attempt at a Python prog (Chess) Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-02-15 13:41 -0500
  Re: First attempt at a Python prog (Chess) Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-02-16 14:39 +0000

csiph-web