int main(){ //iteradores int i,j,q; double times[100]; FILE *temp, *gnu; //inicializando a semente de GNA srand( (unsigned)time(NULL) ); //inicializando raiz struct node *root = __init_node(); root->name = "raiz"; root->age = 10000; struct node *tmp = __init_node(); root->next=tmp; //preenchendo os demais nós for(i=1;i<100000;i++){ tmp->name = "tt"; tmp->age = i; struct node *aux = __init_node(); tmp->next = aux; tmp = aux; } clock_t t; char *n; int time,s=1000; //obtendo tempos de pesquisa for(i=0;i<100;i++){ printf("Indo de %d a %d...\n",i*1000,(i+1)*1000-1); for(q=0;q<100;q++){ tmp = root; time = (rand() % 1000) + (i*1000); t = clock(); for(j=0;j<time;j++){ tmp = tmp->next; } t = clock() - t; times[i] += ((double)t)/CLOCKS_PER_SEC; } } temp = fopen("data.txt","w"); gnu = popen("gnuplot -persistent","w"); for(i=0;i<100;i++){ times[i] = times[i]/100.0; printf("Media do tempo %d: %lf seg\n",i,times[i]); fprintf(temp,"%d %lf\n",i,times[i]); } fprintf(gnu,"set title \"ACESSOS À MEMÓRIA EM C NO LINUX\"\n"); fprintf(gnu,"plot 'data.txt' with lines\n"); return 0; }
int dlist_append(dlist_t *thiz, void *data) { node_t *n; if (NULL == (n = __init_node(data, thiz->size))) goto err0; if (NULL == thiz->head) { n->next = n; n->prev = n; thiz->head = n; } else { n->next = thiz->head; n->prev = thiz->head->prev; n->prev->next = n; n->next->prev = n; } thiz->count++; return 0; err0: return -1; }
int dlist_insert_sort(dlist_t *thiz, void *data, dlist_cmp_t cmp) { int i; node_t *pos; node_t *n; // 找到插入点 pos for (i = 0, pos = thiz->head; i < thiz->count; i++, pos = pos->next) { if (cmp(data, pos->data) <= 0) break; } if (NULL == (n = __init_node(data, thiz->size))) goto err0; // 前插在 pos if (NULL == thiz->head) { n->next = n; n->prev = n; thiz->head = n; } else { n->next = pos; n->prev = pos->prev; n->next->prev = n; n->prev->next = n; } if (i == 0) thiz->head = n; thiz->count++; return 0; err0: return -1; }
int dlist_insert_indx(dlist_t *thiz, void *data, int indx) { if (indx < 0 || indx > thiz->count) return -2; int i; node_t *pos; node_t *n; i = 0, pos = thiz->head; while (i < indx) i++, pos = pos->next; if (NULL == (n = __init_node(data, thiz->size))) goto err0; // 没有节点 if (NULL == pos) { n->prev = n; n->next = n; thiz->head = n; } else { n->next = pos; n->prev = pos->prev; n->prev->next = n; n->next->prev = n; } // 插入到首节点之前 if (0 == indx) thiz->head = n; thiz->count++; return 0; err0: return -1; }