我想通过使用 memoryviews 来加速我的代码。这是我使用的两个类:
cdef class child:
cdef public int[:] move
def __init__(self, move):
self.move = move
cdef class parent:
cdef public:
list children
int[:, :] moves
def __init__(self):
self.children = []
def add_children(self, moves):
cdef int i = 0
cdef int N = len(moves)
for i in range(N):
self.children.append(child(moves[i]))
这是我要检查类是否有效的代码:
temp = []
for i in range(100):
temp.append([i, i+1])
cdef int[:, :] moves = np.asarray(temp, dtype=np.int32)
a = parent()
a.add_children(moves)
for move in moves:
for ch in a.children:
if move == ch.move:
print('ok')
我希望打印 100 个,ok
但我什么也没得到。我知道如果我使用list(move) == list(ch.move)
我可以获得预期的输出,但我不希望循环中的转换开销。
谁能帮我一个有效的方法?如果有人有任何其他可以提高代码速度的建议,我们将不胜感激。