1
22
2010
0

简单测试hash

 

#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;
}

 

 

Category: linux | Tags: | Read Count: 837

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter

Host by is-Programmer.com | Power by Chito 1.3.3 beta | Theme: Aeros 2.0 by TheBuckmaker.com