2017-08-21 9 views
0

大きな入力と2つのネストされたforループを使用するため、処理に時間がかかる関数を使用しています。マルチプロセッシングを使ってPythonで関数を実装する

関数のコード:それは、実行時間を最適化するために、マルチプロセッシングを使用するように、この関数のコードを変更するにはどのように

def transform(self, X): 
     global brands 
     result=[] 
     for x in X: 
      index=0 
      count=0 
      for 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) 

答えて

0

transformの方法でselfの使用が表示されません。だから私は共通の機能を作った。

import re 
import numpy as np 

from concurrent.futures import ProcessPoolExecutor 

def transformer(x): 

    global brands 

    index = 0 
    count = 0 

    for 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): 

    with ProcessPoolExecutor() as executor: 
     result = executor.map(transformer, X) 

    return np.array(list(result)) 
+0

ブランド名は["a"、 "b"、 "c"]とし、transform(["アミン"、 "mejri"])の結果を見たいとしましょう。私はあなたの関数を使用してマルチプロセス:配列(<0x10e8944b0>、dtype =オブジェクトの<ジェネレータオブジェクトresult_iterator)を使用して取得:[(0)、[0] 同じ結果が表示されるはずですか? – camel

+0

申し訳ありませんが、発電機をリストに変換するのを忘れていました。私はコードを更新しました。 – stamaimer

+0

おかげでそれは助けになりました:) – camel

関連する問題