Re: First attempt at a Python prog (Chess)

Discussion in 'Python' started by Ian Kelly, Feb 14, 2013.

  1. Ian Kelly

    Ian Kelly Guest

    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.
    Ian Kelly, Feb 14, 2013
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oscar Benjamin

    Re: First attempt at a Python prog (Chess)

    Oscar Benjamin, Feb 13, 2013, in forum: Python
    Replies:
    0
    Views:
    134
    Oscar Benjamin
    Feb 13, 2013
  2. jkn
    Replies:
    1
    Views:
    98
    Dennis Lee Bieber
    Feb 15, 2013
  3. Rick Johnson
    Replies:
    2
    Views:
    155
    Robert Kern
    Apr 30, 2014
  4. Oscar Benjamin

    Re: First attempt at a Python prog (Chess)

    Oscar Benjamin, Feb 15, 2013, in forum: Python
    Replies:
    4
    Views:
    155
    Neil Cerutti
    Feb 19, 2013
  5. Tim Golden
    Replies:
    0
    Views:
    114
    Tim Golden
    Feb 15, 2013
Loading...

Share This Page