用过程语言实现group by cube的代码
有100万行数据,要求对c1,c2,c3,c4作各种组合的分组汇总,计算行数和v1的合计,即0(c1,c2,c3,c4);
1(c1,c2,c3,NULL);
2(c1,c2,NULL,c4);
3(c1,c2,NULL,NULL);
4(c1,NULL,c3,c4);
5(c1,NULL,c3,NULL);
6(c1,NULL,NULL,c4);
7(c1,NULL,NULL,NULL);
8(NULL,c2,c3,c4);
9(NULL,c2,c3,NULL);
10(NULL,c2,NULL,c4);
11(NULL,c2,NULL,NULL);
12(NULL,NULL,c3,c4);
13(NULL,NULL,c3,NULL);
14(NULL,NULL,NULL,c4);
15(NULL,NULL,NULL,NULL);
/*源数据*/
int main()
{
char* c1=(char*)malloc(1E6L);
char* c2=(char*)malloc(1E6L);
char* c3=(char*)malloc(1E6L);
char* c4=(char*)malloc(1E6L);
int*v1=(int* )malloc(4E6L);
__int64* v2;//store sum(v1);
long t=clock();
for (int i=0;i<1E6L;i++)
{
int j=i+1;
c1=j%4;
c2=j%5;
c3=j%67;
c4=(j%13?j%113:127); //用127模拟NULL
v1=j;
}
用尽量少的空间和时间 这种计算空间占用不大,时间很难再优化了(先把相同的行数统计出来,再汇总) 很多地方没有优化,应该还能提高100毫秒
lenovo t61
453ms,recn=152760
547ms,recn=234600,c1=-1,c2=127,c3=127,c4=127,count=1000000,sum=500000500000
hp dl360
483ms,recn=152760
577ms,recn=234600,c1=-1,c2=127,c3=127,c4=127,count=1000000,sum=500000500000
/* compiled by cl of vc2003*/
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <iostream>
#define add_key {if((key_recn.find(key))== key_recn.end())key_recn=c++;}
#define addkey {if((key_recn2.find(key))== key_recn2.end())key_recn2=c++;}
using namespace std;
struct ltstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) < 0;
}
};
struct ltint
{
bool operator()(const int s1, const int s2) const
{
return (s1<s2) ;
}
};
int main()
{
char* c1=(char*)malloc(1E6L);
char* c2=(char*)malloc(1E6L);
char* c3=(char*)malloc(1E6L);
char* c4=(char*)malloc(1E6L);
int*v1=(int* )malloc(4E6L);
__int64* v2;//store sum(v1);
long t=clock();
for (int i=0;i<1E6L;i++)
{
int j=i+1;
c1=j%4;
c2=j%5;
c3=j%67;
c4=(j%13?j%113:127);
v1=j;
}
map<const int, int, ltint> key_recn;
map<const int, int, ltint> key_recn2;
int c=0;
for (int i=0;i<1E6L;i++)
{
int key=(c1<<24)+(c2<<16)+(c3<<8)+c4;
//printf("key=%d\n",key);
add_key;
}
int *C=(int*)malloc(c*4);//store count(c1)
int *k=(int*)malloc(c*4);//store key
v2=(__int64*)malloc(c*8);
memset(C,0,c*4); //need?
memset(v2,0,c*8);
for (int i=0;i<1E6L;i++)
{
int recn=key_recn[(c1<<24)+(c2<<16)+(c3<<8)+c4];
k=(c1<<24)+(c2<<16)+(c3<<8)+c4;
C++;
v2=v2+v1;
}
printf("%dms,recn=%d\n",clock()-t,c);
t=clock();
int c_2=c;
for (int i=0;i<c_2;i++)
{
int key=(k&0xFFFFFF)+((0x80+15)<<24);//set c1 null ,gid=8
addkey;
key=(k&0xFF00FFFF)+((0x40)<<24)+(127<<16);//set c2 null,gid=4
addkey;
key=(k&0xFFFF00FF)+((0x20)<<24)+(127<<8);//set c3 null,gid=2
addkey;
key=(k&0xFFFFFF00)+((0x10)<<24)+(127);//set c4 null,gid=1
addkey;
key=(k&0xFFFF00)+((0x90+15)<<24)+(127);//set c1,c4 null ,gid=9
addkey;
key=(k&0xFF00FF00)+((0x50)<<24)+(127<<16)+(127);//set c2,c4 null,gid=5
addkey;
key=(k&0xFFFF0000)+((0x30)<<24)+(127<<8)+(127);//set c3,c4 null,gid=3
addkey;
key=(k&0xFF0000FF)+((0x60)<<24)+(127<<16)+(127<<8);//set c2,c3 null,gid=6
addkey;
key=(k&0x00FF00FF)+((0xA0+15)<<24)+(127<<8);//set c1,c3 null,gid=10
addkey;
key=(k&0xFFFF)+((0xC0+15)<<24)+(127<<16);//set c1,c2 null,gid=12
addkey;
key=(k&0xFF00)+((0xD0+15)<<24)+(127<<16)+(127);//set c1,c2,c4 null,gid=13
addkey;
key=(k&0xFF)+((0xE0+15)<<24)+(127<<16)+(127<<8);//set c1,c2,c3 null,gid=14
addkey;
key=(k&0xFF0000)+((0xB0+15)<<24)+(127<<8)+(127);//set c1,c3,c4 null,gid=11
addkey;
key=(k&0xFF000000)+((0x70)<<24)+(127<<16)+(127<<8)+(127);//set c2,c3,c4 null,gid=7
addkey;
key=((0xF0+15)<<24)+(127<<16)+(127<<8)+(127);//set c1,c2,c3,c4 null,gid=15
addkey;
}
int mask={
0x00FFFFFF,
0xFF00FFFF,
0xFFFF00FF,
0xFFFFFF00,
0x00FFFF00,
0xFF00FF00,
0xFFFF0000,
0xFF0000FF,
0x00FF00FF,
0x0000FFFF,
0x0000FF00,
0x000000FF,
0x00FF0000,
0xFF000000,
0x00000000};
int null={
((0x80+15)<<24),
((0x40)<<24)+(127<<16),
((0x20)<<24)+(127<<8),
((0x10)<<24)+(127),
((0x90+15)<<24)+(127),
((0x50)<<24)+(127<<16)+(127),
((0x30)<<24)+(127<<8)+(127),
((0x60)<<24)+(127<<16)+(127<<8),
((0xA0+15)<<24)+(127<<8),
((0xC0+15)<<24)+(127<<16),
((0xD0+15)<<24)+(127<<16)+(127),
((0xE0+15)<<24)+(127<<16)+(127<<8),
((0xB0+15)<<24)+(127<<8)+(127),
((0x70)<<24)+(127<<16)+(127<<8)+(127),
((0xF0+15)<<24)+(127<<16)+(127<<8)+(127)
};
int *C2=(int*)malloc((c-c_2)*4);//store count(c1)
int *k2=(int*)malloc((c-c_2)*4);//store key
__int64* v3=(__int64*)malloc((c-c_2)*8);
memset(C2,0,(c-c_2)*4); //need?
memset(v3,0,(c-c_2)*8);
int y=0;
for (int i=0;i<c_2;i++)
{
for(int j=0;j<15;j++)
{
int recn=key_recn2[(k&mask)+null]-c_2;
k2=(k&mask)+null;
C2+=C;
v3=v3+v2;
y=recn;
}
}
printf("%dms,recn=%d,c1=%d,c2=%d,c3=%d,c4=%d,count=%d,sum=%I64d",clock()-t,c,
(k2>>24),((k2>>16)&255),((k2>>8)&255),(k2&255),C2,v3);
free(c1);
free(c2);
free(c3);
free(c4);
free(v1);
free(v2);
free(C);
free(k);
free(C2);
free(k2);
free(v3);
return 0;
}
/* compiled by cl of vc2003*/
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <iostream>
#define add_key {if((key_recn.find(key))== key_recn.end())key_recn=c++;}
#define addkey {if((key_recn2.find(key))== key_recn2.end())key_recn2=c++;}
#define adkey(i) {if((key_recn2.find(key))== key_recn2.end())key_recn2=c++;}
#define sumkey(i) {f++;int recn=key_recn2-c_2;k2=key;C2+=C2[(iter->second)-c_2];v3+=v3[(iter->second)-c_2];}
#define __int64 long long
using namespace std;
struct ltstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) < 0;
}
};
struct ltint
{
bool operator()(const int s1, const int s2) const
{
return (s1<s2) ;
}
};
int main()
{
int s={ {4,5,67},{9,19,109},{9,97,119}};
//printf("%d,%d",t,t);
//return 0;
for(int r=2;r<3;r++)
{
long t=clock();
printf("%d,%d,%d,114,%d\n",s,s,s,s*s*s*114);
char* c1=(char*)malloc(1E6L);
char* c2=(char*)malloc(1E6L);
char* c3=(char*)malloc(1E6L);
char* c4=(char*)malloc(1E6L);
int*v1=(int* )malloc(4E6L);
printf("申请源表内存%dms\n",clock()-t);
t=clock();
__int64* v2;//store sum(v1);
//FILE *fp=fopen("in.txt","wb");
//FILE *fp=fopen("in.txt","rb");
for (int i=0;i<1E6L;i++)
{
int j=i+1;
c1=j%s;
c2=j%s;
c3=j%s;
c4=(j%13?j%113:127);
v1=j;
//fscanf(fp,"%d,%d,%d,%d,%d\n",&c1,&c2,&c3,&c4,&v1);
//fprintf(fp,"%d,%d,%d,%d,%d\n",c1,c2,c3,c4,v1);
}
//fclose(fp);
//return 1;
map<const int, int, ltint> key_recn;
map<const int, int, ltint> key_recn2;
int c=0;
printf("填充源表数据%dms,recn=%d,每毫秒%d行\n",clock()-t,c,(long)1E6/(clock()-t));
t=clock();
for (int i=0;i<1E6L;i++)
{
int key=(c1<<24)+(c2<<16)+(c3<<8)+c4;
//printf("key=%d\n",key);
add_key;
}
printf("新建一层节点%dms,recn=%d,每毫秒读%d行,每毫秒写%d节点\n",clock()-t,c,(long)1E6/(clock()-t),c/(clock()-t));
t=clock();
int *C=(int*)malloc(c*4);//store count(c1)
int *k=(int*)malloc(c*4);//store key
v2=(__int64*)malloc(c*8);
memset(C,0,c*4); //need?
memset(v2,0,c*8);
printf("申请一层内存%dms\n",clock()-t);
t=clock();
for (int i=0;i<1E6L;i++)
{
int recn=key_recn[(c1<<24)+(c2<<16)+(c3<<8)+c4];
k=(c1<<24)+(c2<<16)+(c3<<8)+c4;
C++;
v2=v2+v1;
}
printf("写入一层数据%dms,recn=%d,每毫秒读%d行,每毫秒写%d行\n",clock()-t,c,(long)1E6/(clock()-t),c/(clock()-t));
t=clock();
int c_2=c;
for (int i=0;i<c_2;i++)
{
int key=(k&0xFFFFFF)+((0x80+15)<<24);//set c1 null ,gid=8
adkey(8);
key=(k&0xFF00FFFF)+((0x40)<<24)+(127<<16);//set c2 null,gid=4
adkey(4);
key=(k&0xFFFF00FF)+((0x20)<<24)+(127<<8);//set c3 null,gid=2
adkey(2);
key=(k&0xFFFFFF00)+((0x10)<<24)+(127);//set c4 null,gid=1
adkey(1);
/*
key=(k&0xFFFF00)+((0x90+15)<<24)+(127);//set c1,c4 null ,gid=9
adkey(9);
key=(k&0xFF00FF00)+((0x50)<<24)+(127<<16)+(127);//set c2,c4 null,gid=5
adkey(5);
key=(k&0xFFFF0000)+((0x30)<<24)+(127<<8)+(127);//set c3,c4 null,gid=3
adkey(3);
key=(k&0xFF0000FF)+((0x60)<<24)+(127<<16)+(127<<8);//set c2,c3 null,gid=6
adkey(6);
key=(k&0x00FF00FF)+((0xA0+15)<<24)+(127<<8);//set c1,c3 null,gid=10
adkey(10);
key=(k&0xFFFF)+((0xC0+15)<<24)+(127<<16);//set c1,c2 null,gid=12
adkey(12);
key=(k&0xFF00)+((0xD0+15)<<24)+(127<<16)+(127);//set c1,c2,c4 null,gid=13
adkey(13);
key=(k&0xFF)+((0xE0+15)<<24)+(127<<16)+(127<<8);//set c1,c2,c3 null,gid=14
adkey(14);
key=(k&0xFF0000)+((0xB0+15)<<24)+(127<<8)+(127);//set c1,c3,c4 null,gid=11
adkey(11);
key=(k&0xFF000000)+((0x70)<<24)+(127<<16)+(127<<8)+(127);//set c2,c3,c4 null,gid=7
adkey(7);
key=((0xF0+15)<<24)+(127<<16)+(127<<8)+(127);//set c1,c2,c3,c4 null,gid=15
adkey(15);
*/
}
map<const int, int,ltint>::iterator iter;
for(int i=1;i<7;i++)
{
if (i==1)
for(iter=key_recn2.begin();iter != key_recn2.end(); iter++)
{
int key=((iter->first)&0x0FFF0000)+((0x30)<<24)+(127<<8)+(127);//set c3,c4 null,gid=3
adkey(3);
key=((iter->first)&0xFFFF00)+((0x90+15)<<24)+(127);//set c1,c4 null ,gid=9
adkey(9);
key=((iter->first)&0x0F00FF00)+((0x50)<<24)+(127<<16)+(127);//set c2,c4 null,gid=5
adkey(5);
}
if(i==2)
for(iter=key_recn2.begin();iter != key_recn2.end(); iter++)
{
int key=((iter->first)&0x00FF00FF)+((0xA0+15)<<24)+(127<<8);//set c1,c3 null,gid=10
adkey(10);
}
if(i==4)
for(iter=key_recn2.begin();iter != key_recn2.end(); iter++)
{
int key=((iter->first)&0x0F0000FF)+((0x60)<<24)+(127<<16)+(127<<8);//set c2,c3 null,gid=6
adkey(6);
key=((iter->first)&0xFFFF)+((0xC0+15)<<24)+(127<<16);//set c1,c2 null,gid=12
adkey(12);
}
if(i==3)
for(iter=key_recn2.begin();iter != key_recn2.end(); iter++)
{
int key=((iter->first)&0xFF0000)+((0xB0+15)<<24)+(127<<8)+(127);//set c1,c3,c4 null,gid=11
adkey(11);
key=((iter->first)&0x0F000000)+((0x70)<<24)+(127<<16)+(127<<8)+(127);//set c2,c3,c4 null,gid=7
adkey(7);
}
if(i==5)
for(iter=key_recn2.begin();iter != key_recn2.end(); iter++)
{
int key=((iter->first)&0xFF00)+((0xD0+15)<<24)+(127<<16)+(127);//set c1,c2,c4 null,gid=13
adkey(13);
}
if(i==6)
for(iter=key_recn2.begin();iter != key_recn2.end(); iter++)
{
int key=((iter->first)&0xFF)+((0xE0+15)<<24)+(127<<16)+(127<<8);//set c1,c2,c3 null,gid=14
adkey(14);
}
}
int key=((0xF0+15)<<24)+(127<<16)+(127<<8)+(127);//set c1,c2,c3,c4 null,gid=15
adkey(15);
printf("新建后面节点%dms,recn=%d,每毫秒读%d行key,每毫秒写%d节点\n",clock()-t,c,c_2/(clock()-t),(c-c_2)/(clock()-t));
for(int i=0;i<16;i++)
{
printf("%d,first=%0X,renc=%d\n",i,key_recn2.begin()->first,key_recn2.begin()->first]);
}
t=clock();
int o={8,4,2,1,9,5,3,6,10,12,13,14,11,7,15};
int mask={
0x00FFFFFF,
0xFF00FFFF,
0xFFFF00FF,
0xFFFFFF00,
0x00FFFF00,
0xFF00FF00,
0xFFFF0000,
0xFF0000FF,
0x00FF00FF,
0x0000FFFF,
0x0000FF00,
0x000000FF,
0x00FF0000,
0xFF000000,
0x00000000};
int null={
((0x80+15)<<24),
((0x40)<<24)+(127<<16),
((0x20)<<24)+(127<<8),
((0x10)<<24)+(127),
((0x90+15)<<24)+(127),
((0x50)<<24)+(127<<16)+(127),
((0x30)<<24)+(127<<8)+(127),
((0x60)<<24)+(127<<16)+(127<<8),
((0xA0+15)<<24)+(127<<8),
((0xC0+15)<<24)+(127<<16),
((0xD0+15)<<24)+(127<<16)+(127),
((0xE0+15)<<24)+(127<<16)+(127<<8),
((0xB0+15)<<24)+(127<<8)+(127),
((0x70)<<24)+(127<<16)+(127<<8)+(127),
((0xF0+15)<<24)+(127<<16)+(127<<8)+(127)
};
int *C2=(int*)malloc((c-c_2)*4);//store count(c1)
int *k2=(int*)malloc((c-c_2)*4);//store key
__int64* v3=(__int64*)malloc((c-c_2)*8);
memset(C2,0,(c-c_2)*4); //need?
memset(v3,0,(c-c_2)*8);
printf("申请后面内存%dms,%d\n",clock()-t,c);
t=clock();
//goto f;
int y=0;
int f;
memset(f,0,15*4);
for (int i=0;i<c_2;i++)
{
for(int j=0;j<4;j++)
{
//int key=(k&mask)+null;
//if(key_recn2].find(key)!= key_recn2].end())
{f]++;
int recn=key_recn2][(k&mask)+null]-c_2;
k2=(k&mask)+null;
C2+=C;
v3+=v2;
y=recn;
}
}
}
for(int i=1;i<7;i++)
{
if (i==1)
for(iter=key_recn2.begin();iter != key_recn2.end(); iter++)
{
int key=((iter->first)&0x0FFF0000)+((0x30)<<24)+(127<<8)+(127);//set c3,c4 null,gid=3
/* {f++;
int recn=key_recn2-c_2;
k2=key;
C2+=C2[(iter->second)-c_2];
v3+=v3[(iter->second)-c_2];
}*/
sumkey(3);
key=((iter->first)&0xFFFF00)+((0x90+15)<<24)+(127);//set c1,c4 null ,gid=9
sumkey(9);
key=((iter->first)&0x0F00FF00)+((0x50)<<24)+(127<<16)+(127);//set c2,c4 null,gid=5
sumkey(5);
}
if(i==2)
for(iter=key_recn2.begin();iter != key_recn2.end(); iter++)
{
int key=((iter->first)&0x00FF00FF)+((0xA0+15)<<24)+(127<<8);//set c1,c3 null,gid=10
sumkey(10);
}
if(i==4)
for(iter=key_recn2.begin();iter != key_recn2.end(); iter++)
{
int key=((iter->first)&0x0F0000FF)+((0x60)<<24)+(127<<16)+(127<<8);//set c2,c3 null,gid=6
sumkey(6);
key=((iter->first)&0xFFFF)+((0xC0+15)<<24)+(127<<16);//set c1,c2 null,gid=12
sumkey(12);
}
if(i==3)
for(iter=key_recn2.begin();iter != key_recn2.end(); iter++)
{
int key=((iter->first)&0xFF0000)+((0xB0+15)<<24)+(127<<8)+(127);//set c1,c3,c4 null,gid=11
sumkey(11);
key=((iter->first)&0x0F000000)+((0x70)<<24)+(127<<16)+(127<<8)+(127);//set c2,c3,c4 null,gid=7
sumkey(7);
key=((0xF0+15)<<24)+(127<<16)+(127<<8)+(127);//set c1,c2,c3,c4 null,gid=15
sumkey(15);
}
if(i==5)
for(iter=key_recn2.begin();iter != key_recn2.end(); iter++)
{
int key=((iter->first)&0xFF00)+((0xD0+15)<<24)+(127<<16)+(127);//set c1,c2,c4 null,gid=13
sumkey(13);
}
if(i==6)
for(iter=key_recn2.begin();iter != key_recn2.end(); iter++)
{
int key=((iter->first)&0xFF)+((0xE0+15)<<24)+(127<<16)+(127<<8);//set c1,c2,c3 null,gid=14
sumkey(14);
}
}
printf("汇总后面节点%dms,recn=%d,每毫秒读%d行,每毫秒写%d行\n",clock()-t,c,c_2/(clock()-t),(c-c_2)/(clock()-t));
t=clock();
/*
printf("%fms,recn=%d,c1=%d,c2=%d,c3=%d,c4=%d,count=%d,sum=%I64d\n",(float)(clock()-t)/CLOCKS_PER_SEC ,c,
(k2>>24),((k2>>16)&255),((k2>>8)&255),(k2&255),C2,v3);
*/
FILE *fp=fopen("out.txt","wb");
//fp=fopen("out.txt","wb");
fprintf(fp,"gid,c1,c2,c3,c4,count,sum\n");
for (int i=0;i<c_2;i++)
{
y=i;
fprintf(fp,"%d,%d,%d,%d,%d,%d,%I64d\n",
0,(k>>24),((k>>16)&255),((k>>8)&255),(k&255),C,v2);
}
for (int i=0;i<c-c_2;i++)
{
y=i;
fprintf(fp,"%d,%d,%d,%d,%d,%d,%I64d\n",
(16+((k2>>24)>>4))&15,(k2>>24)&15,((k2>>16)&255),((k2>>8)&255),(k2&255),C2,v3);
}
fclose(fp);
printf("保存到文件%dms,recn=%d,每毫秒写%d行\n",clock()-t,c,c/(clock()-t));
t=clock();
f:
free(c1);
free(c2);
free(c3);
free(c4);
free(v1);
free(v2);
free(C);
free(k);
free(C2);
free(k2);
free(v3);
printf("释放所有动态内存%dms,%d\n",clock()-t,c);
for(int i=0;i<16;i++)
printf("%d,%d\n",i,f);
}
return 0;
}
rollup的选择还是有讲究的
/* compiled by cl of vc2003*/
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <iostream>
#define add_key {if((key_recn.find(key))== key_recn.end())key_recn=c++;}
#define addkey {if((key_recn2.find(key))== key_recn2.end())key_recn2=c++;}
#define addskey {if((iter=key_recn.find(key))== key_recn.end()){key_recn=c++;int recn=c;k=key;C++;v2+=v1;}\
else{int recn=iter->second;k=key;C++;v2+=v1;}}
/*int recn=key_recn[(c1<<24)+(c2<<16)+(c3<<8)+c4];
k=(c1<<24)+(c2<<16)+(c3<<8)+c4;
C++;
v2=v2+v1;*/
#define adkey(i) {if((key_recn2.find(key))== key_recn2.end())key_recn2=c++;}
#define sumkey(i) {f++;int recn=key_recn2-c_2;k2=key;C2+=C2[(iter->second)-c_2];v3+=v3[(iter->second)-c_2];}
#define adsmkey(j) {if((iter=key_recn2.find(key))== key_recn2.end()){key_recn2=c++;f++;int recn=c-c_2;k2=key;C2+=C;v3+=v2;}\
else{f++;int recn=/*key_recn2-c_2*/iter->second-c_2;k2=key;C2+=C;v3+=v2;}}
#define __int64 long long
using namespace std;
struct ltstr
{
bool operator()(const char* s1, const char* s2) const
{
return strcmp(s1, s2) < 0;
}
};
struct ltint
{
bool operator()(const int s1, const int s2) const
{
return (s1<s2) ;
}
};
int main()
{
int dsize=0;
int s={ {4,5,67},{9,109,19},{9,97,119}};
//printf("%d,%d",t,t);
//return 0;
for(int it=1;it<2;it++)
for(int r=2;r<3;r++)
{
long t=clock();
long t1=t;
printf("%d,%d,%d,114,%d\n",s,s,s,s*s*s*114);
char* c1=(char*)malloc(1E6L);
char* c2=(char*)malloc(1E6L);
char* c3=(char*)malloc(1E6L);
char* c4=(char*)malloc(1E6L);
int*v1=(int* )malloc(4E6L);
printf("申请源表内存%dms\n",clock()-t);
t=clock();
__int64* v2;//store sum(v1);
//FILE *fp=fopen("in.txt","wb");
//FILE *fp=fopen("in.txt","rb");
for (int i=0;i<1E6L;i++)
{
int j=i+1;
c1=j%s;
c2=j%s;
c3=j%s;
c4=(j%13?j%113:127);
v1=j;
//fscanf(fp,"%d,%d,%d,%d,%d\n",&c1,&c2,&c3,&c4,&v1);
//fprintf(fp,"%d,%d,%d,%d,%d\n",c1,c2,c3,c4,v1);
}
//fclose(fp);
//return 1;
map<const int, int, ltint> key_recn;
map<const int, int, ltint> key_recn2;
map<const int, int,ltint>::iterator iter;
int c=1E6L;
printf("填充源表数据%dms,recn=%d,每毫秒%d行\n",clock()-t,c,(long)1E6/(clock()-t));
t=clock();
int *C=(int*)malloc(c*4);//store count(c1)
int *k=(int*)malloc(c*4);//store key
v2=(__int64*)malloc(c*8);
memset(C,0,c*4); //need?
memset(v2,0,c*8);
printf("申请一层内存%dms\n",clock()-t);
t=clock();
c=0;
for (int i=0;i<1E6L;i++)
{
int key=(c1<<24)+(c2<<16)+(c3<<8)+c4;
//printf("key=%d\n",key);
addskey;
}
printf("新建一层节点%dms,recn=%d,每毫秒读%d行,每毫秒写%d节点\n",clock()-t,c,(long)1E6/(clock()-t),c/(clock()-t));
t=clock();
/*
for (int i=0;i<1E6L;i++)
{
int recn=key_recn[(c1<<24)+(c2<<16)+(c3<<8)+c4];
k=(c1<<24)+(c2<<16)+(c3<<8)+c4;
C++;
v2=v2+v1;
}
*/
//printf("写入一层数据%dms,recn=%d,每毫秒读%d行,每毫秒写%d行\n",clock()-t,c,(long)1E6/(clock()-t),c/(clock()-t));
t=clock();
int c_2=c;
int o={8,4,2,1,9,5,3,6,10,12,13,14,11,7,15};
int mask={
0x00FFFFFF,
0xFF00FFFF,
0xFFFF00FF,
0xFFFFFF00,
0x00FFFF00,
0xFF00FF00,
0xFFFF0000,
0xFF0000FF,
0x00FF00FF,
0x0000FFFF,
0x0000FF00,
0x000000FF,
0x00FF0000,
0xFF000000,
0x00000000};
int null={
((0x80+15)<<24),
((0x40)<<24)+(127<<16),
((0x20)<<24)+(127<<8),
((0x10)<<24)+(127),
((0x90+15)<<24)+(127),
((0x50)<<24)+(127<<16)+(127),
((0x30)<<24)+(127<<8)+(127),
((0x60)<<24)+(127<<16)+(127<<8),
((0xA0+15)<<24)+(127<<8),
((0xC0+15)<<24)+(127<<16),
((0xD0+15)<<24)+(127<<16)+(127),
((0xE0+15)<<24)+(127<<16)+(127<<8),
((0xB0+15)<<24)+(127<<8)+(127),
((0x70)<<24)+(127<<16)+(127<<8)+(127),
((0xF0+15)<<24)+(127<<16)+(127<<8)+(127)
};
dsize=c_2;
int *C2=(int*)malloc((c_2)*4);//store count(c1)
int *k2=(int*)malloc((c_2)*4);//store key
__int64* v3=(__int64*)malloc((c_2)*8);
memset(C2,0,(c_2)*4); //need?
memset(v3,0,(c_2)*8);
printf("申请后面内存%dms,%d\n",clock()-t,c_2);
t=clock();
//goto f;
int y=0;
int f;
memset(f,0,16*4);
for (int i=0;i<c_2;i++)
{
if(dsize<c-c_2+4)
{
dsize+=dsize;
C2=(int*)realloc(C2,(dsize)*4);//store count(c1)
k2=(int*)realloc(k2,(dsize)*4);//store key
v3=(__int64*)realloc(v3,(dsize)*8);
//memset(C2,0,(c_2)*4); //need?
//memset(v3,0,(c_2)*8);
}
int key=(k&0xFFFFFF)+((0x80+15)<<24);//set c1 null ,gid=8
adsmkey(8);
key=(k&0xFF00FFFF)+((0x40)<<24)+(127<<16);//set c2 null,gid=4
adsmkey(4);
key=(k&0xFFFF00FF)+((0x20)<<24)+(127<<8);//set c3 null,gid=2
adsmkey(2);
key=(k&0xFFFFFF00)+((0x10)<<24)+(127);//set c4 null,gid=1
adsmkey(1);
/*
for(int j=0;j<4;j++)
{
//int key=(k&mask)+null;
//if(key_recn2].find(key)!= key_recn2].end())
{f]++;
int recn=key_recn2][(k&mask)+null]-c_2;
k2=(k&mask)+null;
C2+=C;
v3+=v2;
y=recn;
}
}
key=(k&0xFFFF00)+((0x90+15)<<24)+(127);//set c1,c4 null ,gid=9
adkey(9);
key=(k&0xFF00FF00)+((0x50)<<24)+(127<<16)+(127);//set c2,c4 null,gid=5
adkey(5);
key=(k&0xFFFF0000)+((0x30)<<24)+(127<<8)+(127);//set c3,c4 null,gid=3
adkey(3);
key=(k&0xFF0000FF)+((0x60)<<24)+(127<<16)+(127<<8);//set c2,c3 null,gid=6
adkey(6);
key=(k&0x00FF00FF)+((0xA0+15)<<24)+(127<<8);//set c1,c3 null,gid=10
adkey(10);
key=(k&0xFFFF)+((0xC0+15)<<24)+(127<<16);//set c1,c2 null,gid=12
adkey(12);
key=(k&0xFF00)+((0xD0+15)<<24)+(127<<16)+(127);//set c1,c2,c4 null,gid=13
adkey(13);
key=(k&0xFF)+((0xE0+15)<<24)+(127<<16)+(127<<8);//set c1,c2,c3 null,gid=14
adkey(14);
key=(k&0xFF0000)+((0xB0+15)<<24)+(127<<8)+(127);//set c1,c3,c4 null,gid=11
adkey(11);
key=(k&0xFF000000)+((0x70)<<24)+(127<<16)+(127<<8)+(127);//set c2,c3,c4 null,gid=7
adkey(7);
key=((0xF0+15)<<24)+(127<<16)+(127<<8)+(127);//set c1,c2,c3,c4 null,gid=15
adkey(15);
*/
}
//map<const int, int,ltint>::iterator iter;
switch(it)
{
case 1:
for(int i=1;i<7;i++)
{
if (i==1)
for(iter=key_recn2.begin();iter != key_recn2.end(); iter++)
{
int key=((iter->first)&0x0FFF0000)+((0x30)<<24)+(127<<8)+(127);//set c3,c4 null,gid=3
adkey(3);
key=((iter->first)&0xFFFF00)+((0x90+15)<<24)+(127);//set c1,c4 null ,gid=9
adkey(9);
key=((iter->first)&0x0F00FF00)+((0x50)<<24)+(127<<16)+(127);//set c2,c4 null,gid=5
adkey(5);
}
if(i==2)
for(iter=key_recn2.begin();iter != key_recn2.end(); iter++)
{
int key=((iter->first)&0x00FF00FF)+((0xA0+15)<<24)+(127<<8);//set c1,c3 null,gid=10
adkey(10);
}
if(i==4)
for(iter=key_recn2.begin();iter != key_recn2.end(); iter++)
{
int key=((iter->first)&0x0F0000FF)+((0x60)<<24)+(127<<16)+(127<<8);//set c2,c3 null,gid=6
adkey(6);
key=((iter->first)&0xFFFF)+((0xC0+15)<<24)+(127<<16);//set c1,c2 null,gid=12
adkey(12);
}
if(i==3)
for(iter=key_recn2.begin();iter != key_recn2.end(); iter++)
{
int key=((iter->first)&0xFF0000)+((0xB0+15)<<24)+(127<<8)+(127);//set c1,c3,c4 null,gid=11
adkey(11);
key=((iter->first)&0x0F000000)+((0x70)<<24)+(127<<16)+(127<<8)+(127);//set c2,c3,c4 null,gid=7
adkey(7);
}
if(i==5)
for(iter=key_recn2.begin();iter != key_recn2.end(); iter++)
{
int key=((iter->first)&0xFF00)+((0xD0+15)<<24)+(127<<16)+(127);//set c1,c2,c4 null,gid=13
adkey(13);
}
if(i==6)
for(iter=key_recn2.begin();iter != key_recn2.end(); iter++)
{
int key=((iter->first)&0xFF)+((0xE0+15)<<24)+(127<<16)+(127<<8);//set c1,c2,c3 null,gid=14
adkey(14);
}
}
break;
case 2:
for(int i=1;i<7;i++)
{
if (i==1)
for(iter=key_recn2.begin();iter != key_recn2.end(); iter++)
{
}
if(i==2)
for(iter=key_recn2.begin();iter != key_recn2.end(); iter++)
{
int key=((iter->first)&0x0FFF0000)+((0x30)<<24)+(127<<8)+(127);//set c3,c4 null,gid=3
adkey(3);
key=((iter->first)&0xFFFF00)+((0x90+15)<<24)+(127);//set c1,c4 null ,gid=9
adkey(9);
key=((iter->first)&0x0F00FF00)+((0x50)<<24)+(127<<16)+(127);//set c2,c4 null,gid=5
adkey(5);
key=((iter->first)&0x00FF00FF)+((0xA0+15)<<24)+(127<<8);//set c1,c3 null,gid=10
adkey(10);
}
if(i==4)
for(iter=key_recn2.begin();iter != key_recn2.end(); iter++)
{
int key=((iter->first)&0x0F0000FF)+((0x60)<<24)+(127<<16)+(127<<8);//set c2,c3 null,gid=6
adkey(6);
key=((iter->first)&0xFFFF)+((0xC0+15)<<24)+(127<<16);//set c1,c2 null,gid=12
adkey(12);
}
if(i==3)
for(iter=key_recn2.begin();iter != key_recn2.end(); iter++)
{
int key=((iter->first)&0xFF0000)+((0xB0+15)<<24)+(127<<8)+(127);//set c1,c3,c4 null,gid=11
adkey(11);
key=((iter->first)&0x0F000000)+((0x70)<<24)+(127<<16)+(127<<8)+(127);//set c2,c3,c4 null,gid=7
adkey(7);
}
if(i==5)
for(iter=key_recn2.begin();iter != key_recn2.end(); iter++)
{
int key=((iter->first)&0xFF00)+((0xD0+15)<<24)+(127<<16)+(127);//set c1,c2,c4 null,gid=13
adkey(13);
}
if(i==6)
for(iter=key_recn2.begin();iter != key_recn2.end(); iter++)
{
int key=((iter->first)&0xFF)+((0xE0+15)<<24)+(127<<16)+(127<<8);//set c1,c2,c3 null,gid=14
adkey(14);
}
}
break;
default:break;
}
int key=((0xF0+15)<<24)+(127<<16)+(127<<8)+(127);//set c1,c2,c3,c4 null,gid=15
adkey(15);
printf("新建后面节点%dms,recn=%d,每毫秒读%d行key,每毫秒写%d节点\n",clock()-t,c,c_2/(clock()-t),(c-c_2)/(clock()-t));
for(int i=0;i<16;i++)
{
printf("%d,first=%0X,renc=%d\n",i,key_recn2.begin()->first,key_recn2.begin()->first]);
}
t=clock();
/*
for (int i=0;i<c_2;i++)
{
for(int j=0;j<4;j++)
{
//int key=(k&mask)+null;
//if(key_recn2].find(key)!= key_recn2].end())
{f]++;
int recn=key_recn2][(k&mask)+null]-c_2;
k2=(k&mask)+null;
C2+=C;
v3+=v2;
y=recn;
}
}
}
*/
for(int i=1;i<7;i++)
{
if (i==1)
for(iter=key_recn2.begin();iter != key_recn2.end(); iter++)
{
int key=((iter->first)&0x0FFF0000)+((0x30)<<24)+(127<<8)+(127);//set c3,c4 null,gid=3
/* {f++;
int recn=key_recn2-c_2;
k2=key;
C2+=C2[(iter->second)-c_2];
v3+=v3[(iter->second)-c_2];
}*/
sumkey(3);
key=((iter->first)&0xFFFF00)+((0x90+15)<<24)+(127);//set c1,c4 null ,gid=9
sumkey(9);
key=((iter->first)&0x0F00FF00)+((0x50)<<24)+(127<<16)+(127);//set c2,c4 null,gid=5
sumkey(5);
}
if(i==2)
for(iter=key_recn2.begin();iter != key_recn2.end(); iter++)
{
int key=((iter->first)&0x00FF00FF)+((0xA0+15)<<24)+(127<<8);//set c1,c3 null,gid=10
sumkey(10);
}
if(i==4)
for(iter=key_recn2.begin();iter != key_recn2.end(); iter++)
{
int key=((iter->first)&0x0F0000FF)+((0x60)<<24)+(127<<16)+(127<<8);//set c2,c3 null,gid=6
sumkey(6);
key=((iter->first)&0xFFFF)+((0xC0+15)<<24)+(127<<16);//set c1,c2 null,gid=12
sumkey(12);
}
if(i==3)
for(iter=key_recn2.begin();iter != key_recn2.end(); iter++)
{
int key=((iter->first)&0xFF0000)+((0xB0+15)<<24)+(127<<8)+(127);//set c1,c3,c4 null,gid=11
sumkey(11);
key=((iter->first)&0x0F000000)+((0x70)<<24)+(127<<16)+(127<<8)+(127);//set c2,c3,c4 null,gid=7
sumkey(7);
key=((0xF0+15)<<24)+(127<<16)+(127<<8)+(127);//set c1,c2,c3,c4 null,gid=15
sumkey(15);
}
if(i==5)
for(iter=key_recn2.begin();iter != key_recn2.end(); iter++)
{
int key=((iter->first)&0xFF00)+((0xD0+15)<<24)+(127<<16)+(127);//set c1,c2,c4 null,gid=13
sumkey(13);
}
if(i==6)
for(iter=key_recn2.begin();iter != key_recn2.end(); iter++)
{
int key=((iter->first)&0xFF)+((0xE0+15)<<24)+(127<<16)+(127<<8);//set c1,c2,c3 null,gid=14
sumkey(14);
}
}
printf("汇总后面节点%dms,recn=%d,每毫秒读%d行,每毫秒写%d行\n",clock()-t,c,c_2/(clock()-t),(c-c_2)/(clock()-t));
t=clock();
/*
printf("%fms,recn=%d,c1=%d,c2=%d,c3=%d,c4=%d,count=%d,sum=%I64d\n",(float)(clock()-t)/CLOCKS_PER_SEC ,c,
(k2>>24),((k2>>16)&255),((k2>>8)&255),(k2&255),C2,v3);
FILE *fp=fopen("out.txt","wb");
//fp=fopen("out.txt","wb");
fprintf(fp,"gid,c1,c2,c3,c4,count,sum\n");
for (int i=0;i<c_2;i++)
{
y=i;
fprintf(fp,"%d,%d,%d,%d,%d,%d,%I64d\n",
0,(k>>24),((k>>16)&255),((k>>8)&255),(k&255),C,v2);
}
for (int i=0;i<c-c_2;i++)
{
y=i;
fprintf(fp,"%d,%d,%d,%d,%d,%d,%I64d\n",
(16+((k2>>24)>>4))&15,(k2>>24)&15,((k2>>16)&255),((k2>>8)&255),(k2&255),C2,v3);
}
fclose(fp);
printf("保存到文件%dms,recn=%d,每毫秒写%d行\n",clock()-t,c,c/(clock()-t));
t=clock();
*/
f:
free(c1);
free(c2);
free(c3);
free(c4);
free(v1);
free(v2);
free(C);
free(k);
free(C2);
free(k2);
free(v3);
printf("释放所有动态内存%dms,%d\n",clock()-t,c);
for(int i=0;i<16;i++)
printf("%d,%d\n",i,f);
printf("总时间%dms,%d\n",clock()-t1,c);
}
return 0;
}
源代码比较结果 酒向知己饮
可惜我不会饮酒,
:'( 所有的代码 map9d
页:
[1]