6

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

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

4

2 回答 2

3

我可能会使用numpy.array_equal. (它接受 memoryviews 以及 numpy 数组。)

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

memcmp(另一个答案暗示)的主要优点是它memcmp不适用于非连续数组(另一个答案承认)。只需获取一列 2D memoryview 即可轻松获得非连续的 memoryview。

于 2017-08-06T08:51:18.817 回答
1

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

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')

但是,如果内存视图具有不同的 dtypes、字节顺序或步幅,这可能(可能)会导致问题——因为memcmp只是比较普通内存。

于 2017-08-05T11:12:17.670 回答