Converting NumPy array to a set takes too long

后端 未结 2 1350
别那么骄傲
别那么骄傲 2020-12-06 01:56

I\'m trying to execute the following

from numpy import *
x = array([[3,2,3],[711,4,104],.........,[4,4,782,7845]])  # large nparray
for item in x:
    set(it         


        
2条回答
  •  误落风尘
    2020-12-06 02:12

    Here is a way to speed things up: avoid the loop and use a multiprocessing pool.map trick

    from multiprocessing.dummy import Pool as ThreadPool
    import multiprocessing
    
    pool = ThreadPool(multiprocessing.cpu_count()) # get the number of CPU
    y = pool.map(set,x) # apply the function to your iterable
    pool.close()
    pool.join()
    

提交回复
热议问题