use multiprocessing to implement a function in python -


i using function take time finish since takes large input , use 2 nested loops .

the code of function :

def transform(self, x):         global brands         result=[]         x in x:             index=0             count=0             brand in brands:                 all_matches= re.findall(re.escape(brand), x,flags=re.i)                 count_all_match=len(all_matches)                 if(count_all_match>count):                     count=count_all_match                     index=brands.index(brand)              result.append([index])         return np.array(result) 

so how change code of function uses multiprocessing in order optimize running time ?

i don't see use of self in method transform. made common function.

import re import numpy np  concurrent.futures import processpoolexecutor  def transformer(x):      global brands      index = 0     count = 0      brand in brands:          all_matches = re.findall(re.escape(brand), x, flags=re.i)          count_all_match = len(all_matches)          if count_all_match > count:              count = count_all_match              index = brands.index(brand)      return [index]  def transform(x):      processpoolexecutor() executor:         result = executor.map(transformer, x)      return np.array(list(result)) 

Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -