Check if two numpy arrays are identical

前端 未结 7 985
轻奢々
轻奢々 2020-12-10 15:47

Suppose I have a bunch of arrays, including x and y, and I want to check if they\'re equal. Generally, I can just use np.all(x == y) (

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-10 16:47

    Probably someone who understands the underlying data structure could optimize this or explain whether it's reliable/safe/good practice, but it seems to work.

    np.all(a==b)
    Out[]: True
    
    memoryview(a.data)==memoryview(b.data)
    Out[]: True
    
    %timeit np.all(a==b)
    The slowest run took 10.82 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 6.2 µs per loop
    
    %timeit memoryview(a.data)==memoryview(b.data)
    The slowest run took 8.55 times longer than the fastest. This could mean that an intermediate result is being cached.
    100000 loops, best of 3: 1.85 µs per loop
    

    If I understand this correctly, ndarray.data creates a pointer to the data buffer and memoryview creates a native python type that can be short-circuited out of the buffer.

    I think.

    EDIT: further testing shows it may not be as big a time-improvement as shown. previously a=b=np.eye(5)

    a=np.random.randint(0,10,(100,100))
    
    b=a.copy()
    
    %timeit np.all(a==b)
    The slowest run took 6.70 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000 loops, best of 3: 17.7 µs per loop
    
    %timeit memoryview(a.data)==memoryview(b.data)
    10000 loops, best of 3: 30.1 µs per loop
    
    np.all(a==b)
    Out[]: True
    
    memoryview(a.data)==memoryview(b.data)
    Out[]: True
    

提交回复
热议问题