Using Python's multiprocessing to calculate the sum of integers from one long input line
<p>I want to use Python's multiprocessing module for the following:
Map an input line to a list of integers and calculate the sum of this list.</p>

<p>The input line is initially a string where the items to be summed are separated by spaces.</p>

<p>What I have tried is this:</p>

<pre><code>from itertools import imap

my_input = '1000000000 ' * int(1e6)
print sum(imap(int, my_input.split()))
</code></pre>

<p>This takes about 600ms on my machine, but I would like to make it faster with multiprocessing.</p>

<p>It seems that the bottleneck is in the mapping-part since the sum-method is pretty fast when applied to a ready list of integers:</p>

<pre><code>&gt;&gt;&gt; int_list = [int(1e9)] * int(1e6)
&gt;&gt;&gt; %time sum(int_list)
CPU times: user 7.38 ms, sys: 5 Âµs, total: 7.38 ms
Wall time: 7.4 ms
&gt;&gt;&gt; 1000000000000000
</code></pre>

<p>I tried to apply the instructions from <a href="http://stackoverflow.com/questions/29785427/how-to-parallel-sum-a-loop-using-multiprocessing-in-python">this question</a> but as I'm quite new to using multiprocessing, I couldn't fit the instructions to this problem.</p>

<p>So, this seems to roughly boil down to three steps:</p>

<ol>
<li>Make a <a href="https://docs.python.org/3/library/multiprocessing.html#multiprocessing.pool.Pool" rel="nofollow">pool</a></li>
<li>Map int() across the list within that pool</li>
<li>Sum the results.</li>
</ol>

<p>So:</p>

<pre><code>if __name__ == '__main__':
    import multiprocessing
    my_input = '1000000000 ' * int(1e6)
    string_list = my_input.split()
    # Pool over all CPUs
    int_list = multiprocessing.Pool().map(int, string_list)
    print sum(int_list)
</code></pre>

<p>It may be more efficient for time to use generators where possible:</p>

<pre><code>if __name__ == '__main__':
    import multiprocessing
    import re
    my_input = '1000000000 ' * int(1e6)
    # use a regex iterator matching whitespace
    string_list = (x.group(0) for x in re.finditer(r'[^\s]+\s', my_input))
    # Pool over all CPUs
    int_list = multiprocessing.Pool().imap(int, string_list)
    print sum(int_list)
</code></pre>

<p>The regex will likely be slower than <code>split</code>, but using <code>re.finditer</code> should allow the <code>Pool</code> to start mapping as fast as individual splits are made, and using <code>imap</code> rather than <code>map</code> should do similarly for <code>sum</code> (allowing it to start adding numbers as they become available). Credit to <a href="http://stackoverflow.com/a/9770397/6051861">this answer</a> for the <code>re.finditer</code> idea.</p>

<p>It may or may not be more efficient to multiprocess than doing it in a single process. You might end up losing more time making new processes and passing the results back from them than you gain in doing things all at once. The same goes for if you were to try putting the adding into the pool as well.</p>

<p>On the system I'm testing this on, which has two CPUs, I get the one-process solution to run in about half a second, the non-generator multiprocess solution in about 1 second, and the generator solution in 12-13 seconds.</p>

python python-2.7 python-multiprocessing