How to make huge loops in python faster on mac?
<p>I am a computer science student and some of the things I do require me to run huge loops on Macbook with dual core i5. Some the loops take 5-6 hours to complete but they only use 25% of my CPU. Is there a way to make this process faster? I cant change my loops but is there a way to make them run faster?</p>

<p>Thank you</p>

<p>Mac OS 10.11
Python 2.7 (I have to use 2.7) with IDLE or Spyder on Anaconda</p>

<p>Here is a sample code that takes 15 minutes:</p>

<pre><code>def test_false_pos():
    sumA = [0] * 1000
    for test in range(1000):
        counter = 0
        bf = BloomFilter(4095,10)
        for i in range(600):
            bf.rand_inserts()
        for x in range(10000):
            randS = str(rnd.randint(0,10**8))
            if bf.lookup(randS):
                counter += 1
        sumA[test] = counter/10000.0
    avg = np.mean(sumA)
    return avg
</code></pre>

<p>You could split it up into 4 loops:</p>

<pre><code>import multiprocessing

def test_false_pos(i, pieces, q):
    sumA = [0] * 1000
    for test in range((1000/pieces) * i, 250 + (1000/pieces)*i):
        counter = 0
        bf = BloomFilter(4095,10)
        for i in range(600):
            bf.rand_inserts()
        for x in range(10000):
            randS = str(rnd.randint(0,10**8))
            if bf.lookup(randS):
                counter += 1
        sumA[test] = counter/10000.0
    q.put(list(sumA))

def full_test(pieces):
    threads = []
    q = multiprocessing.Queue()
    for i in range(pieces):
        threads.append(multiprocessing.Process(target=test_false_pos, args=(i, pieces, q)))
    [thread.start() for thread in threads]
    sums = []
    for i in range(4):
        sums.append(q.get())
    return np.mean(np.array(sums))

if __name__ == '__main__':
    full_test(4)
</code></pre>

<p>This will run 4 processes that each do Â¼ of the work.</p>

python performance python-2.7 loops cpu-usage