Optimize Prime Sieve

/// A heavily optimized sieve
#include 
#include 
#include 
#include 
typedef unsigned int u32;
typedef unsigned long long ull;
const char pr60[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59};
const char masks[][4]={
    {3,7,11,13},
    {3,17,19,23},
    {2,29,31},
    {2,37,41},
    {2,43,47},
    {2,53,59}
};
const u32 segsize=65536;
void Apply_mask(u32*a,u32*b,u32 l1,u32 l2){
    u32 t=0;
    for(u32 q=0,r=l1/l2;q>1,rt=0;
    while(rt=30)st-=30,++rt;
        if(st>=30)st-=30,++rt;
    }
}
void PrintMask(u32*a,u32 len){
    printf("Mask of len %u\n",len*60);
    for(u32 i=0;i>1;
        if(!(sieve[v]&(1<>1;
            while(rt=30)st-=30,++rt;
            }
            pr[prl][0]=v;
            pr[prl][1]=u;
            pr[prl][2]=rt;
            pr[prl][3]=st;
            prl++;
        }
    } // Non-segmented sieve core
    if(preseg==tmx)goto end;
    for(u32 segl=preseg;segl>1;
            if(!(sieve[v]&(1<>1;
                pr[prl][0]=v;
                pr[prl][1]=u;
                pr[prl][2]=rt;
                pr[prl][3]=st;
                prl++;
            }
        }
        for(int i=0;i=30)st-=30,++rt;
            }
            pr[i][0]=v;
            pr[i][1]=u;
            pr[i][2]=rt;
            pr[i][3]=st;
        }
    }
    end:
    sieve[0]=pr60_m;
    int count=1;
    for(u32 i=0;ima;a-=2){
        u32 i=a/60,j=a%60>>1;
        if(!(sieve[i]&(1<

一个Eratosthenes筛。单线程,筛1e9<0.5s,程序运行时间<0.7s。(7700k 4.2GHz)
(注意后面的统计部分效率极其低下,可用查表位运算替代。)

0.只留奇数项
1.压位,30pack int
2.对于<60的素数用很快的筛子一遍
3.分段筛法

你可能感兴趣的:(Optimize Prime Sieve)