On Thu, Feb 14, 2013 at 10:48 AM, Chris Hinsley <chris.hins...@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. -- http://mail.python.org/mailman/listinfo/python-list