I'm making a chess engine in python so I need to optimize everything to run as fast as possible. When black and white sides are mirrored i need to miror list of scores on squares.
This is a simplified list:
A = [1,2,3,4,5,6,7,8,9]
Let's imagine this list is a matrix:
A = [
1,2,3,
4,5,6,
7,8,9]
What is the fastest way of changing "rows" in this list, so the final result would be:
A = [
7,8,9,
4,5,6,
1,2,3]
or written in the same line:
A = [7,8,9,4,5,6,1,2,3]
Real lists have 64 elements and I need to reverse 8 "rows" in them. This reversing is done milion times so every improvement is welcome. I'm looking for fastest way to do this. Thank you!
Edit:
I've done it this way, is there any faster way of doing this?
A = [1,2,3,4,5,6,7,8,9]
A_size = len(A)
row_length = 3
for x in range(0,A_size,row_length):
A.extend(A[A_size-row_length-x:A_size-x])
del A[0:A_size]
print(A)
Exploring some options:
numpy
should be able to do this efficiently if you use reshape with your list:
import numpy
A = [1,2,3,4,5,6,7,8,9]
m = numpy.array(A).reshape(3,3)
print(numpy.flip(m, axis=0))
The convenience method flipud
does the same, and as mentioned there, you use indexing to achieve the same:
print(numpy.array(A).reshape(3,3)[::-1,...])
The numpy variants turn out to be surprisingly inefficient, perhaps because of the matrix creation overhead. So, a pure-Python alternative to your extend/del loop might be to create a new list from a generator expression:
print([e for r in (A[A_size-row_length-x:A_size-x]
for x in range(0,A_size,row_length)) for e in r])
Another variant makes use of Python's "in-place" slice replacement, and it seems to be the fastest of my variants listed so far:
for x in range(0,A_size//2,row_length):
A[x:x+row_length], A[A_size-row_length-x:A_size-x] = A[A_size-row_length-x:A_size-x], A[x:x+row_length]
However, in my local tests, this last variant is still ~10% slower than your original extend/del variant, and, even more interestingly, this relation seems stable, ie, your original variant is still the fastest of these, when increasing the list size to 50*50.
As a side-node: If it's an option, I see rather dramatic speed-ups using PyPy . I tried out a full index-wise in-place replacement:
A[0],A[1],A[2],A[3],A[4],A[5],A[6],A[7],A[8],A[9],A[10],A[11],A[12],A[13],A[14],A[15],A[16],A[17],A[18],A[19],A[20],A[21],A[22],A[23],A[24],A[25],A[26],A[27],A[28],A[29],A[30],A[31],A[32],A[33],A[34],A[35],A[36],A[37],A[38],A[39],A[40],A[41],A[42],A[43],A[44],A[45],A[46],A[47],A[48],A[49],A[50],A[51],A[52],A[53],A[54],A[55],A[56],A[57],A[58],A[59],A[60],A[61],A[62],A[63] = A[56],A[57],A[58],A[59],A[60],A[61],A[62],A[63],A[48],A[49],A[50],A[51],A[52],A[53],A[54],A[55],A[40],A[41],A[42],A[43],A[44],A[45],A[46],A[47],A[32],A[33],A[34],A[35],A[36],A[37],A[38],A[39],A[24],A[25],A[26],A[27],A[28],A[29],A[30],A[31],A[16],A[17],A[18],A[19],A[20],A[21],A[22],A[23],A[8],A[9],A[10],A[11],A[12],A[13],A[14],A[15],A[0],A[1],A[2],A[3],A[4],A[5],A[6],A[7]
While this is slightly slower than the slice replacement variant above with the standard Python3, it outperforms the original extend/del loop by a factor of around 2.6 with PyPy.
When you want to transform a list and reverse it as being a matrix, you could use the following snippet.
def chunk(lst, n):
for i in range(0, len(lst), n):
yield lst[i:i + n]
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
b = list(chunk(a, 3))
b.reverse()
b = sum(b, [])
print(b)
# [7, 8, 9, 4, 5, 6, 1, 2, 3]
When using two-dimensional arrays as a starting point, this task is much easier:
a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
a.reverse()
print(a)
# [[7, 8, 9], [4, 5, 6], [1, 2, 3]]
Try this, which uses a generator to chunk backward from the end of the list to the beginning:
from itertools import chain
A = [1,2,3,4,5,6,7,8,9]
def chunk(lst, step):
start = len(lst)
for i in range(start, -1, -step):
yield lst[i-step:i]
yield lst[0:i] #any remaning bit
l = list(chain.from_iterable(chunk(A, 3)))
print(l)
Output:
[7, 8, 9, 4, 5, 6, 1, 2, 3]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.