Python top N word count, por qué multiproceso más lento que un solo proceso
Estoy haciendo un recuento de palabras de frecuencia usando python, la versión de proceso único:
#coding=utf-8
import string
import time
from collections import Counter
starttime = time.clock()
origin = open("document.txt", 'r').read().lower()
for_split = [',','\n','\t','\'','.','\"','!','?','-', '~']
#the words below will be ignoered when counting
ignored = ['the', 'and', 'i', 'to', 'of', 'a', 'in', 'was', 'that', 'had',
'he', 'you', 'his','my', 'it', 'as', 'with', 'her', 'for', 'on']
i=0
for ch in for_split:
origin = string.replace(origin, ch, ' ')
words = string.split(origin)
result = Counter(words).most_common(40)
for word, frequency in result:
if not word in ignored and i < 10:
print "%s : %d" % (word, frequency)
i = i+1
print time.clock() - starttime
entonces la versión de multiprocesamiento se ve como:
#coding=utf-8
import time
import multiprocessing
from collections import Counter
for_split = [',','\n','\t','\'','.','\"','!','?','-', '~']
ignored = ['the', 'and', 'i', 'to', 'of', 'a', 'in', 'was', 'that', 'had',
'he', 'you', 'his','my', 'it', 'as', 'with', 'her', 'for', 'on']
result_list = []
def worker(substr):
result = Counter(substr)
return result
def log_result(result):
result_list.append(result)
def main():
pool = multiprocessing.Pool(processes=5)
origin = open("document.txt", 'r').read().lower()
for ch in for_split:
origin = origin.replace(ch, ' ')
words = origin.split()
step = len(words)/4
substrs = [words[pos : pos+step] for pos in range(0, len(words), step)]
result = Counter()
for substr in substrs:
pool.apply_async(worker, args=(substr,), callback = log_result)
pool.close()
pool.join()
result = Counter()
for item in result_list:
result = result + item
result = result.most_common(40)
i=0
for word, frequency in result:
if not word in ignored and i < 10:
print "%s : %d" % (word, frequency)
i = i+1
if __name__ == "__main__":
starttime = time.clock()
main()
print time.clock() - starttime
el "document.txt" es aproximadamente 22M, mi computadora portátil tiene núcleos, memoria 2G, el resultado de la primera versión es 3.27s, y la segunda es 8.15s, he cambiado el número de procesos (pool = multiprocessing.Pool (procesos = 5)), de 2 a 10, los resultados siguen siendo casi iguales, ¿por qué es eso, cómo puedo hacer que este programa funcione más rápido que la versión de proceso único?