STL 常数测试(粗略)

vector - 手写链表

vector和手写链表的时间对比
push_back时间如下

..不开O2开O2
vector8s6s
手写链表4s4s

遍历一次的时间都是2s
详见代码

#pragma GCC optimize(2)
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<ctime>
#define aut(v,j) for (unsigned int j=0;j<v.size();j++)
#define rep(a,b,c) for (int a=b;a<=c;a++)
#define per(a,b,c) for (int a=b;a>=c;a--)
#define go(u) for (int o=ft[u],v;v=E[o].t;o=E[o].n)
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> par;
typedef double dob;
const int lim=3000000,n=100000;
unsigned int seed=19260817;
vector <int> to[n+10];
inline int num(){
    seed=(seed<<17)^(seed<<5)^seed;
    return seed%n+1;
}
#define gettime printf("%d\n",time(0))
int main(){
    gettime;
    rep(_,1,100){
        rep(i,1,n) to[i].clear();
        rep(i,1,lim) to[num()].push_back(num());
    }
    gettime;
    rep(_,1,1000){
        rep(i,1,n) aut(to[i],j) to[i][j]=233;
    }
    gettime;
    return 0;
}
#pragma GCC optimize(2)
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<ctime>
#define aut(v,j) for (unsigned int j=0;j<v.size();j++)
#define rep(a,b,c) for (int a=b;a<=c;a++)
#define per(a,b,c) for (int a=b;a>=c;a--)
#define go(u) for (int o=ft[u],v;v=E[o].t;o=E[o].n)
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> par;
typedef double dob;
const int lim=3000000,n=100000;
unsigned int seed=19260817;
struct edge{int t,n;}E[lim+10];
int vis[n+10],ft[n+10],tot;
inline int num(){
    seed=(seed<<17)^(seed<<5)^seed;
    return seed%n+1;
}
void add(int x,int y){
    E[++tot]=(edge){y,ft[x]},ft[x]=tot;
}
#define gettime printf("%d\n",time(0))
int main(){
    gettime;
    rep(_,1,100){
        tot=0;
        memset(ft,0,sizeof(ft));
        rep(i,1,lim) add(num(),num());
    }
    gettime;
    rep(_,1,1000){
        tot=0;
        rep(i,1,n) go(i) vis[v]=233;
    }
    gettime;
    return 0;
}

预处理log - math库log

一开始担心math库的log太慢,拖慢rmq速度
然而测试了一下,速度几乎一样,在1e次随机查询下都是9s
开O2都没有变快

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<ctime>
#include<cmath>
#define rep(a,b,c) for (int a=b;a<=c;a++)
#define per(a,b,c) for (int a=b;a>=c;a--)
#define go(u) for (int o=ft[u],v;v=E[o].t;o=E[o].n)
#define fi first
#define se second
using namespace std;
typedef long long LL;
typedef pair<int,int> par;
typedef double dob;
int lo[1000010];
inline int rd(){return rand()*10000+rand();}
int main(){
    int lim=100000000,n=1000000;
    printf("%d\n",time(0));
    rep(i,2,n) lo[i]=lo[i>>1]+1;
    rep(i,1,lim){
        int x=lo[(rd()%n+n)%n+1];
        i+=x;
        i-=x;
    }
    printf("%d\n",time(0));
    rep(i,1,lim){
        int x=(rd()%lim+lim)%lim+1,
            y=(int)(log((double)(x))/log(2.0));
        i+=y;
        i-=y;
    }
    printf("%d\n",time(0));
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值