Optimizing primes generating program in C++ I am writing a program to generate primes in range 1 upto 10^9 but it seems to be timing out. 

It implements the Sieve of Atkin

I have tried optimizing my code using bitsets and pragmas.

Please are there any other ways i can optimize my code. Code below 

#include&lt;bitset&gt;
#include&lt;vector&gt;
#include&lt;iostream&gt;
#include&lt;algorithm&gt;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using namespace std;
#define int long long
const int LIM = 1e9;
bitset&lt;100000000&gt; sieve;
void compute(int limit) 
{ 
    if (limit &gt; 2) 
        cout&lt;&lt;"2 ";
    if (limit &gt; 3) 
        cout&lt;&lt;"3 ";

    for (int x = 1; x * x &lt; limit; x++) { 
        for (int y = 1; y * y &lt; limit; y++) { 
            int n = (4 * x * x) + (y * y); 
            if (n &lt;= limit &amp;&amp; (n % 12 == 1 || n % 12 == 5)) 
                sieve.flip(n); 

            n = (3 * x * x) + (y * y); 
            if (n &lt;= limit &amp;&amp; n % 12 == 7) 
                sieve.flip(n);

            n = (3 * x * x) - (y * y); 
            if (x &gt; y &amp;&amp; n &lt;= limit &amp;&amp; n % 12 == 11) 
                sieve.flip(n);
        } 
    } 
    for (int r = 5; r * r &lt; limit; r++) { 
        if (sieve.test(r)) { 
            for (int i = r * r; i &lt; limit; i += r * r) 
                sieve.reset(i);
        } 
    } 
    for(int i=sieve._Find_first();i&lt; sieve.size();i = sieve._Find_next(i))
    {
        if(i==0||i==1)continue;
        cout&lt;&lt;i&lt;&lt;" ";
    }
    cout&lt;&lt;endl;
}
signed main()
{
    compute(LIM);
    return 0;    
}


Thanks in advance.
I'm not sure what your optimizations are doing(comments would be nice), but I did notice several inefficiencies:

Never include a calculation in the limit test in a for loop.  It get's re-calculated on every iteration.  In this case set the limit to sqrt(limit).  the calculation is kind of expensive but it's done only once.

The same goes for the iteration step, calculate it once before you enter the loop.

In the tests, in the first set of loops, you use 3*x*x twice and y*y 3 times.  putting these in variables would be a lot cheaper.