Filling a list faster

I have a small block of code that I use to populate the list with integers. I need to improve its performance, perhaps translate it all into arrays numpy, but I'm not sure how to do it.

Here MWE:

import numpy as np

# List filled with integers.
a = np.random.randint(0,100,1000)

N = 10
b = [[] for _ in range(N-1)]
for indx,integ in enumerate(a):
    if 0<elem<N:
        b[integ-1].append(indx)

This is what it does:

  • for each integer ( integ) ina
  • see if it is between a given range (0,N)
  • if so, store its index in a sub-list b, where the index of the specified sub-list is the original integer minus 1 ( integ-1)

This bit of code works pretty fast, but my actual code uses much larger lists, therefore, its performance needs to be improved.

+4
2

! , , N.

def new(a, N):
    mask = (a > 0) & (a < N)
    elements = a[mask]
    indices = np.arange(a.size)[mask]

    sorting_idx = np.argsort(elements, kind='mergesort')
    ind_sorted = indices[sorting_idx]

    x = np.searchsorted(elements, range(N), side='right', sorter=sorting_idx)

    return [ind_sorted[x[i]:x[i+1]] for i in range(N-1)]

x = x.tolist() , (NB: a = a.tolist() , ). , 'mergesort', , , .

+2

:

mask = (a > 0) & (a < N)
elements = a[mask]
indicies = np.arange(a.size)[mask]

b = [indicies[elements == i] for i in range(1, N)]

:

import numpy as np

a = np.random.randint(0,100,1000)
N = 10

def original(a, N):
    b = [[] for _ in range(N-1)]
    for indx,elem in enumerate(a):
        if 0<elem<N:
            b[elem-1].append(indx)
    return b

def new(a, N):
    mask = (a > 0) & (a < N)
    elements = a[mask]
    indicies = np.arange(a.size)[mask]

    return [indicies[elements == i] for i in range(1, N)]

"" (~ 20x) :

In [5]: %timeit original(a, N)
100 loops, best of 3: 1.21 ms per loop

In [6]: %timeit new(a, N)
10000 loops, best of 3: 57 us per loop

:

In [7]: new_results = new(a, N)

In [8]: old_results = original(a, N)

In [9]: for x, y in zip(new_results, old_results):
   ....:     assert np.allclose(x, y)
   ....:

In [10]:        

"" . a , 1 , 17 ( ~ 70x).

+4

Source: https://habr.com/ru/post/1527783/


All Articles