循环检查两个内存视图的有效方法是什么?

What is the efficient way to check two memoryviews in loop?

我想通过使用内存视图来加速我的代码。这里有两个 类 我用:

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) 我可以获得预期的输出,但我不希望循环中的转换开销。

任何人都可以帮助我有效的方法吗?如果有人有任何其他提高代码速度的建议,我们将不胜感激。

您可以利用 c 库中的 memcmp(比较内存的函数):

from libc.string cimport memcmp

cdef int[:, :] moves = np.asarray(temp, dtype=np.int32)
cdef int[:] move
cdef child ch
a = parent()
a.add_children(moves)
for move in moves:
    for ch in a.children:
        if memcmp(&move[0], &ch.move[0], move.nbytes) == 0:
            print('ok')

然而,如果内存视图具有不同的数据类型、字节顺序或跨度,这可能(可能)导致问题 - 因为 memcmp 只是比较普通内存。

我可能会使用 numpy.array_equal。 (它接受内存视图和 numpy 数组。)

if numpy.array_equal(move, ch.move):
  print("OK")

memcmp 相比的主要优势(另一个答案表明)是 memcmp 不适用于非连续数组(另一个答案承认)。只需取一个二维内存视图的列,就可以很容易地得到一个非连续的内存视图。