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>Sure thing: Python 2.7 has to generate huge lists and waste a lot of memory <em>each time</em> you use <code>range(&lt;a huge number&gt;)</code>. </p>

<p>Try to use the <code>xrange</code> function instead. It doesn't create that gigantic list at once, it produces the members of a sequence <em>lazily</em>. </p>

<hr>

<p>But if your were to use Python 3 (which is the modern version and the future of Python), you'll find out that there <code>range</code> is even cooler and faster than <code>xrange</code> in Python 2.</p>

python performance python-2.7 loops cpu-usage