4.
素朴な実装 3
frequency = defaultdict(int)
for line in opened_file:
for word in some_splitter(line):
frequency[word] += 1
for word in frequency:
some_output(word, frequency[word])
ファイル
frequency
メモリに
辞書/ハッシュ/
連想配列/Mapを
持つ頻度(Pythonic)
frequency = collections.Counter(
word
for line in opened_file
for word in some_splitter(line))
for word, count in frequency.iteritems():
some_output(word, count)
5.
巨大なファイルだと…… 4
:
for line in opened_file:
for word in some_splitter(line):
frequency[word] += 1
:
巨大な
ファイル メモリが
足りない!!
7.
出力を分割する 6
frequency = defaultdict(int)
for line in opend_file:
for word in some_splitter(line):
if hash(word) % 2 == 1:
frequency[word] += 1
:
frequency = defaultdict(int)
for line in opend_file:
for word in some_splitter(line):
if hash(word) % 2 == 0:
frequency[word] += 1
:
巨大な
ファイル
頻度
頻度
ハッシュの
剰余で間引いて
半分に
ある単語は一方の
ファイルにだけ存在
単純に結合すればOK
もう一度読む
残り半分
8.
組み合わせる 7
f = [defaultdict(int)
for i in range(2)]
for l in of:
for w in sp(l):
f[hash(w) % 2]
[w] += 1
ファイル
ファイル
同じ単語が
入っている
ファイル同士を
統合する
剰余で分割
ファイルを
分割
単純な
結合でOK
並列可能
26.
かんたん並行処理
a.reduceは2プロセスで
b.reduceは1プロセスで実行される
Pythonプロセスと実行時間で確認
25
(Python)
# 0から999999999までの総和を計算する
from operator import add
a = sc.parallelize(xrange(1000000000), 2)
b = sc.parallelize(xrange(1000000000), 1)
a.reduce(add)
499999999500000000
b.reduce(add)
499999999500000000
27.
かんたん並行処理
Scalaではスレッドが使われる
26
(Scala)
// 0から999999999までの総和を計算する
val a = sc.parallelize(1L until 1000000000, 2)
val b = sc.parallelize(1L until 1000000000, 1)
a.reduce(_ + _)
499999999500000000
b.reduce(_ + _)
499999999500000000
Be the first to comment