Note that there are some explanatory texts on larger screens.

plurals
  1. POPython, using multiprocess is slower than not using it
    text
    copied!<p>After spending a lot of time trying to wrap my head around multiprocessing I came up with this code which is a benchmark test:</p> <p><strong>Example 1:</strong></p> <pre><code>from multiprocessing import Process class Alter(Process): def __init__(self, word): Process.__init__(self) self.word = word self.word2 = '' def run(self): # Alter string + test processing speed for i in range(80000): self.word2 = self.word2 + self.word if __name__=='__main__': # Send a string to be altered thread1 = Alter('foo') thread2 = Alter('bar') thread1.start() thread2.start() # wait for both to finish thread1.join() thread2.join() print(thread1.word2) print(thread2.word2) </code></pre> <p>This completes in 2 seconds (half the time of multithreading). Out of curiosity I decided to run this next:</p> <p><strong>Example 2:</strong></p> <pre><code>word2 = 'foo' word3 = 'bar' word = 'foo' for i in range(80000): word2 = word2 + word word = 'bar' for i in range(80000): word3 = word3 + word print(word2) print(word3) </code></pre> <p>To my horror this ran in less than half a second!</p> <p>What is going on here? I expected multiprocessing to run faster - shouldn't it complete in half Example 2's time given that Example 1 is Example 2 split into two processes?</p> <h2>Update:</h2> <p>After considering Chris' feedback, I have included the 'actual' code consuming the most process time, and lead me to consider multiprocessing:</p> <pre><code>self.ListVar = [[13379+ strings],[13379+ strings], [13379+ strings],[13379+ strings]] for b in range(len(self.ListVar)): self.list1 = [] self.temp = [] for n in range(len(self.ListVar[b])): if not self.ListVar[b][n] in self.temp: self.list1.insert(n, self.ListVar[b][n] + '(' + str(self.ListVar[b].count(self.ListVar[b][n])) + ')') self.temp.insert(0, self.ListVar[b][n]) self.ListVar[b] = list(self.list1) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload