double timetest(char *alg,int n){ srand(time(NULL)); int *arr=malloc(sizeof(int)*n); int i; stimer_start(); for(i=0;i<n;i++) arr[i]=rand()%10000; if(strcmp(alg,"qsort")==0) qsort(arr,n,sizeof(int),comp); stimer_stop(); free(arr); return stimer_time_total(); }
/*测试某数据是否在有序数组里面*/ void bintest(int len){ item_t x; item_t *arr=malloc(sizeof(item_t)*len); /*随机生成数据,并进行排序*/ random_array(arr,len,1000000); qsort(arr,len,sizeof(item_t),item_cmp); x.key=5677; printf("Searching the key %d: ",x.key); stimer_start(); if(binsearch(&x,arr,len,sizeof(item_t),item_cmp)){ printf("\t Found! \n"); } else{ printf("\t Not Found! \n"); } stimer_stop(); printf("%5.1f\n",stimer_time_total()); free(arr); }
/*单次测试*/ double timetest(char *alg,item_t *arr,int len,item_t x){ item_t *r1; if(strcmp(alg,"bsearch")) { stimer_start(); r1=bsearch(&x,arr,len,sizeof(item_t),item_cmp); if(!r1){ printf("Can't found!\n"); exit(1); } stimer_stop(); } if(strcmp(alg,"binsearch")) { stimer_start(); r1=binsearch(&x,arr,len,sizeof(item_t),item_cmp); if(!r1){ printf("Can't found!\n"); exit(1); } stimer_stop(); } return stimer_time_total(); }