Saturday 15 February 2014

python - multithreading running slower than single thread? -



python - multithreading running slower than single thread? -

i'm trying profile basic function in python see comparative advantage of multithreading evaluating results. seems threaded version performs increasingly worse size of info across function applied increases. there overhead in starting threads i'm not taking business relationship here? can explain how accomplish multithreaded optimization / i'm doing wrong?

from multiprocessing import pool def f(x): homecoming x*x pool = pool(processes=4) import timeit print timeit.timeit('map(f, range(20000000))', setup = "from __main__ import f", number = 1) print timeit.timeit('pool.map(f, range(20000000))', setup = "from __main__ import f, pool", number = 1)

results:

5.90005707741 11.8840620518 [finished in 18.9s]

if relevant, ran in sublime text 3.

the "unit of work" in each job way small. concern when "map" jobs this--the overhead of mapping process dominates. of course of study mapping job separate process more time consuming mapping in same process, no surprise multiprocess solution slower.

try function lot more computation , see benefits of multiprocessing.

python multithreading

No comments:

Post a Comment