📄 test.c
字号:
#include <stdio.h>#include <stdlib.h>#include <sys/time.h>#include "hash.h"typedef struct { char uid[20]; char name[10]; char addr[20]; int i0;} sub_info_t;typedef struct Hash<sub_info_t> SubHash;/* 1000 means hash queue size */SubHash gHashQueue(1000);/* for performance compare */sub_info_t queue[10000];main(){ sub_info_t sub; struct timeval start_time; struct timeval end_time; /* add 10000 into HashMemory*/ for(int i = 0; i < 10000; i ++) { sprintf(sub.uid,"8888%05d",i); sprintf(sub.name,"%d",i); sprintf(sub.addr,"add%d",i); sub.i0 = i; if(gHashQueue.hashInsertElement(&sub) < 0) { fprintf(stderr,"Error insert into hash,i = %d\n",i); exit(0); } memcpy((void *)&queue[i],(void *)&sub,sizeof(sub_info_t)); } /* test case - 0 : performance compare */ fprintf(stderr,"case - 0(performace test)\n"); /* search for user 888805555 */ memset((void *)&sub,0,sizeof(sub_info_t)); strcpy(sub.uid,"888805555"); sub_info_t *pSub; gettimeofday(&start_time,NULL); pSub = gHashQueue.hashFindElement(&sub); gettimeofday(&end_time,NULL); if(!pSub) { fprintf(stderr,"don't find the subscriber %s\n",pSub->uid); } else { fprintf(stderr,"hashFind:uid = %s name = %s i0 = %d\n",pSub->uid,pSub->name,pSub->i0); fprintf(stderr,"hashFind: exceed time %d useconds\n", (end_time.tv_sec - start_time.tv_sec)*1000000 + (end_time.tv_usec - start_time.tv_usec)); } gettimeofday(&start_time,NULL); for(int i = 0; i < 10000;i ++) { if(!strcmp(queue[i].uid,sub.uid)) { gettimeofday(&end_time,NULL); fprintf(stderr,"seqnFind:uid = %s name = %s i0 = %d\n",queue[i].uid,queue[i].name,queue[i].i0); fprintf(stderr,"seqnFind: exceed time %d useconds\n", (end_time.tv_sec - start_time.tv_sec)*1000000 + (end_time.tv_usec - start_time.tv_usec)); break; } } /* test case - 1 : delete */ fprintf(stderr,"test case - 1 : delete\n"); if(gHashQueue.hashDeleteElement(&sub) < 0) { fprintf(stderr,"delete sub %s failed\n",sub.uid); } else { fprintf(stderr,"delete sub %s successful\n",sub.uid); } pSub = gHashQueue.hashFindElement(&sub); if(pSub) { fprintf(stderr,"successful find sub %s\n",sub.uid); } else { fprintf(stderr,"failed to find sub %s\n",sub.uid); } fprintf(stderr,"test over\n");}/* need provided by invoke */unsigned int SubHash::hashKey(sub_info_t *pSub){ /* get the last five digits to be the hash key */ /* it is decided by programer */ char *p; p = pSub->uid + strlen(pSub->uid) - 5; if(p) return atol(p); return 0;}/* need provided by invoke */int SubHash::cmpFunc(sub_info_t *pSub0,sub_info_t *pSub1){ return strcmp(pSub0->uid,pSub1->uid);};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -