Speed up a list operation using threads in python
<p>I have a huge list of proxies (70k) and I have this script:</p>

<pre><code>entries = open("proxy.txt").readlines()
proxiesp = [x.strip().split(":") for x in entries] 
proxies = []
for x in proxiesp:
    x = tuple(x)
    proxies.append(x)
    set(proxies)
</code></pre>

<p>And the operation of set(proxies) so the duplicates removing, is really slow. Is there a way to speed up this using threads?</p>

<p>No, threading won't speed this up, for three reasons:</p>

<ol>
<li><p>The Python GIL prevents Python code from being executed in parallel; threads executing Python code can only be run concurrently. For the same amount of CPU work, the same amount of time or more is required.</p></li>
<li><p>To be able to add to the same datastructure from multiple threads, you'd have to add locking, slowing down threading more.</p></li>
<li><p>Your code is slow because it is wasting cycles, because you are recreating the set object each iteration and then discarding it again. This is sucking up all the time as <code>proxies</code> continues to grow, so in the end you created sets for each size of <code>proxies</code>, from length 1 all the way up to length 70k, approaching 5 million steps to throw away 70k sets.</p></li>
</ol>

<p>You should produce the set <strong>once</strong>. You can do so in a set comprehension:</p>

<pre><code>with open('proxy.txt') as f:
    proxies = {tuple(line.strip().split(':')) for line in f}
</code></pre>

python multithreading list python-3.x set