#include <stdio.h> #include <stdlib.h> #include <search.h> #include <string.h> #define NUM_EMPL 100000 int main(int argc, char * argv[]) { int i; struct timeval tv; struct timeval tv_e; if( (argc > 1) && (strcmp(argv[1], "hash") == 0) ) { ENTRY item; ENTRY *found_item; hcreate(NUM_EMPL); for( i = 0; i < NUM_EMPL; i++ ) { item.key = (char *)malloc(16); item.data = (char *)malloc(16); sprintf(item.key, "%d", i); sprintf(item.data, "%d", i); hsearch(item, ENTER); } gettimeofday(&tv, NULL); item.key = "50000"; if((found_item = hsearch(item, FIND)) != NULL) { printf( "found itaa!\n" ); } else { printf( "no such data!\n" ); } gettimeofday(&tv_e, NULL); printf("hash (%ld)us \n", (1000000*(tv_e.tv_sec - tv.tv_sec) + tv_e.tv_usec - tv.tv_usec)); } else { int data_array[NUM_EMPL]; int find_key = 50000; for(i = 0; i < NUM_EMPL; i++) { data_array[i] = i; } gettimeofday(&tv, NULL); for(i = 0; i < NUM_EMPL; i++) { if( data_array[i] == find_key ) { printf( "found it!\n" ); break; } } gettimeofday(&tv_e, NULL); printf("(%ld)us \n", (1000000*(tv_e.tv_sec - tv.tv_sec) + tv_e.tv_usec - tv.tv_usec)); } return 0; }