2016-05-03 16 views
1

がかかります。
このループは、時間のランニングをたくさん取っている:夜7時38分にはまだ完了していない後読む比較的大きなファイルは、私が<strong>265メガバイト<strong>テキスト</strong>の</strong>と行のファイルを持っている時間

B = namedtuple("B", ["id", "ch"]) 

def get_tuple_from_b_string(b_str): 
    return B(int(b_str.split("_")[0]),int(b_str.split("_")[1])) 

with open("/tmp/file.out") as test_out: 
    for line in test_out: 
     if line == '' or not line.startswith("["): 
       continue 
     bracket_index = line.find(']') 
     b_str = line[1:bracket_index] 
     content = line[bracket_index+1:] 
     b_tuple = get_tuple_from_b_string(b_str) 
     if not b_tuple in b_tupled_list: 
      continue 
     if not b_tuple in b_outputs: 
      b_outputs[b_tuple] = '' 
      b_outputs[b_tuple] += content+'\n' 

私は今、まだ実行しています。
iがプロセスのstraceのを確認しようとし、それは行の繰り返し:

mmap2(NULL、3145728、PROT_READ | PROT_WRITE、MAP_PRIVATE | MAP_ANONYMOUS、 -1、0xff9093fc)= 0xfffffffff4997000
と、munmap(0xf678b000 、3145728)

しかし、異なるアドレス(私はそれが固まっていないと仮定し、読んでいます)。
私の質問はです:

  • なぜそれが(そのが立ち往生していないという仮定の下で)長い時間がかかっていますか?
  • それがついている場合は、どうすればわかりますか?ファイルコンテンツの

例:

[1_03]{ 
[1_03] "0": { 
[1_03]  "interfaces": { 
[1_03]   "address": [], 
[1_03]   "distribution": [], 
[1_03]   "interface": [], 
[1_03]   "netmask": [] 
[1_03]  }, 
+0

データが膨大であるため、時間がかかります。私はあなたが行ごとに読んでいるのを見ています。その代わりに、 'read()'を使って一度読み込み、その行を繰り返し処理してみてください。 –

+0

ファイルからいくつかの例の行を表示できますか? –

+0

@AnmolSinghJaggi NO!ファイルオブジェクトを直接反復することは、ファイル全体をラムに読み込み、行に分割してそのリストを反復するよりも優れています。 'read()'を使うと、265MBのデータ全体がメモリにロードされた1つの文字列として取得されます。また、プログラムはデータの処理を開始する前に読み取りが完了するのを待っています。ファイルに数GBのRAMがあり、そのRAMがそれより小さい場合はどうなりますか?運が悪い!ファイルオブジェクトを反復処理すると、ループごとにRAMに1行しか読み込まれず、はるかに高速で非常にメモリ効率が向上します。 –

答えて

0

いくつかのパフォーマンスの最適化:あなたは少し速くなるべきこれらの最適化により

def get_tuple_from_b_string(b_str): 
    # do not split the string twice, map and unpack the split list at once: 
    return B(*map(int, b_str.split("_", 2))) 

with open("/tmp/file.out") as test_out: 
    for line in test_out: 
     # omit superfluous test and reverse if logic to remove the 'continue' 
     # (probably more a matter of style than performance though...) 
     if line.startswith("["): 
      # replace finding plus twice slicing with one slice and a split: 
      b_str, content = line[1:].split("]", 2) 
      b_tuple = get_tuple_from_b_string(b_str) 
      # again reversing the if-logic to omit the 'continue'. 
      # maybe this could be sped up using a set, depending on what you're doing... 
      if b_tuple in b_tupled_list: 
       # 'a not in b' is more clear than 'not a in b' 
       if b_tuple not in b_outputs: 
        # omit the empty assignment, string concatenation is slow! 
        b_outputs[b_tuple] = content+'\n' 

。特に文字列操作はおそらく最も高価な部分です。なぜなら文字列は不変であり、文字列を変更しようとするとその文字列のコピーが作成されるからです。これには時間とメモリがかかり、パフォーマンス上重要なスクリプトでは避けるべきです。

0

各行にJSON以外のプレフィックスを付けてタグ付けした別々のJSONドキュメントが多数あります。まず、awkを使用してファイルを1_03.jsonという名前の複数の有効なJSONファイルに分割します。その後、私はすべてujsonhttps://pypi.python.org/pypi/ujson)のような高速JSONパーサを使ってPythonに読み込みました。これは全体的にはるかに速いでしょう。

awkスクリプトはおおよそ(軽くテストした)このようなことがあります

BEGIN { 
    FS="]"; 
} 

{ 
    filename=substr($1, 2)".json"; # e.g. 1_03.json 
    $1=""; 
    print > filename; 
} 

は、その後、それはPythonでファイルをINGのとujsonで解析するglobの問題です。

関連する問題

 関連する問題