Posted on 2011-11-04 14:21
Shuffy 閱讀(654)
評論(0) 編輯 收藏 引用
Hash查找因為其O(1)的查找性能而著稱,被對查找性能要求高的應用所廣泛采用。它的基本思想是:
(1) 創建一個定長的線性Hash表,一般可以初始化時指定length;
(2) 設計Hash函數,將關鍵字key散射到Hash表中。其中hash函數設計是最為關鍵的,均勻分布、沖突概率小全在它;
(3) 通常采用拉鏈方法來解決hash沖突問題,即散射到同一個hash表項的關鍵字,以鏈表形式來表示(也稱為桶backet);
(4) 給定關鍵字key,就可以在O(1) + O(m)的時間復雜度內定位到目標。其中,m為拉鏈長度,即桶深。
Hash應用中,字符串是最為常見的關鍵字,應用非常普通,現在的程序設計語言中基本上都提供了字符串hash表的支持。字符串hash函數非常多,常見的主要有Simple_hash, RS_hash, JS_hash, PJW_hash, ELF_hash, BKDR_hash, SDBM_hash, DJB_hash, AP_hash, CRC_hash等。它們的C語言實現見后面附錄代碼: hash.h, hash.c。那么這么些字符串hash函數,誰好熟非呢?評估hash函數優劣的基準主要有以下兩個指標:
(1) 散列分布性
即桶的使用率backet_usage = (已使用桶數) / (總的桶數),這個比例越高,說明分布性良好,是好的hash設計。
(2) 平均桶長
即avg_backet_len,所有已使用桶的平均長度。理想狀態下這個值應該=1,越小說明沖突發生地越少,是好的hash設計。
hash函數計算一般都非常簡潔,因此在耗費計算時間復雜性方面判別甚微,這里不作對比。
評估方案設計是這樣的:
(1) 以200M的視頻文件作為輸入源,以4KB的塊為大小計算MD5值,并以此作為hash關鍵字;
(2) 分別應用上面提到的各種字符串hash函數,進行hash散列模擬;
(3) 統計結果,用散列分布性和平均桶長兩個指標進行評估分析。
測試程序見附錄代碼hashtest.c,測試結果如下表所示。從這個結果我們也可以看出,這些字符串hash函數真是不相仲伯,難以決出高低,所以實際應用中可以根據喜好選擇。當然,最好實際測試一下,畢竟應用特點不大相同。其他幾組測試結果也類似,這里不再給出。
Hash函數 |
桶數 |
Hash調用總數 |
最大桶長 |
平均桶長 |
桶使用率% |
simple_hash |
10240 |
47198 |
16 |
4.63 |
99.00% |
RS_hash |
10240 |
47198 |
16 |
4.63 |
98.91% |
JS_hash |
10240 |
47198 |
15 |
4.64 |
98.87% |
PJW_hash |
10240 |
47198 |
16 |
4.63 |
99.00% |
ELF_hash |
10240 |
47198 |
16 |
4.63 |
99.00% |
BKDR_hash |
10240 |
47198 |
16 |
4.63 |
99.00% |
SDBM_hash |
10240 |
47198 |
16 |
4.63 |
98.90% |
DJB_hash |
10240 |
47198 |
15 |
4.64 |
98.85% |
AP_hash |
10240 |
47198 |
16 |
4.63 |
98.96% |
CRC_hash |
10240 |
47198 |
16 |
4.64 |
98.77% |
附錄源代碼:
hash.h
- #ifndef _HASH_H
- #define _HASH_H
- #ifdef __cplusplus
- extern "C" {
- #endif
-
- unsigned int simple_hash(char *str);
-
- unsigned int RS_hash(char *str);
-
- unsigned int JS_hash(char *str);
-
- unsigned int PJW_hash(char *str);
-
- unsigned int ELF_hash(char *str);
-
- unsigned int BKDR_hash(char *str);
-
- unsigned int SDBM_hash(char *str);
-
- unsigned int DJB_hash(char *str);
-
- unsigned int AP_hash(char *str);
-
- unsigned int CRC_hash(char *str);
- #ifdef __cplusplus
- }
- #endif
- #endif
hash.c
hashtest.c
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <string.h>
- #include "hash.h"
- #include "md5.h"
- struct hash_key {
- unsigned char *key;
- struct hash_key *next;
- };
- struct hash_counter_entry {
- unsigned int hit_count;
- unsigned int entry_count;
- struct hash_key *keys;
- };
- #define BLOCK_LEN 4096
- static int backet_len = 10240;
- static int hash_call_count = 0;
- static struct hash_counter_entry *hlist = NULL;
- unsigned int (*hash_func)(char *str);
- void choose_hash_func(char *hash_func_name)
- {
- if (0 == strcmp(hash_func_name, "simple_hash"))
- hash_func = simple_hash;
- else if (0 == strcmp(hash_func_name, "RS_hash"))
- hash_func = RS_hash;
- else if (0 == strcmp(hash_func_name, "JS_hash"))
- hash_func = JS_hash;
- else if (0 == strcmp(hash_func_name, "PJW_hash"))
- hash_func = PJW_hash;
- else if (0 == strcmp(hash_func_name, "ELF_hash"))
- hash_func = ELF_hash;
- else if (0 == strcmp(hash_func_name, "BKDR_hash"))
- hash_func = BKDR_hash;
- else if (0 == strcmp(hash_func_name, "SDBM_hash"))
- hash_func = SDBM_hash;
- else if (0 == strcmp(hash_func_name, "DJB_hash"))
- hash_func = DJB_hash;
- else if (0 == strcmp(hash_func_name, "AP_hash"))
- hash_func = AP_hash;
- else if (0 == strcmp(hash_func_name, "CRC_hash"))
- hash_func = CRC_hash;
- else
- hash_func = NULL;
- }
- void insert_hash_entry(unsigned char *key, struct hash_counter_entry *hlist)
- {
- unsigned int hash_value = hash_func(key) % backet_len;
- struct hash_key *p;
- p = hlist[hash_value].keys;
- while(p) {
- if (0 == strcmp(key, p->key))
- break;
- p = p->next;
- }
- if (p == NULL)
- {
- p = (struct hash_key *)malloc(sizeof(struct hash_key));
- if (p == NULL)
- {
- perror("malloc in insert_hash_entry");
- return;
- }
- p->key = strdup(key);
- p->next = hlist[hash_value].keys;
- hlist[hash_value].keys = p;
- hlist[hash_value].entry_count++;
- }
- hlist[hash_value].hit_count++;
- }
- void hashtest_init()
- {
- int i;
- hash_call_count = 0;
- hlist = (struct hash_counter_entry *) malloc (sizeof(struct hash_counter_entry) * backet_len);
- if (NULL == hlist)
- {
- perror("malloc in hashtest_init");
- return;
- }
- for (i = 0; i < backet_len; i++)
- {
- hlist[i].hit_count = 0;
- hlist[i].entry_count = 0;
- hlist[i].keys = NULL;
- }
- }
- void hashtest_clean()
- {
- int i;
- struct hash_key *pentry, *p;
- if (NULL == hlist)
- return;
- for (i = 0; i < backet_len; ++i)
- {
- pentry = hlist[i].keys;
- while(pentry)
- {
- p = pentry->next;
- if (pentry->key) free(pentry->key);
- free(pentry);
- pentry = p;
- }
- }
- free(hlist);
- }
- void show_hashtest_result()
- {
- int i, backet = 0, max_link = 0, sum = 0;
- int conflict_count = 0, hit_count = 0;
- float avg_link, backet_usage;
- for(i = 0; i < backet_len; i++)
- {
- if (hlist[i].hit_count > 0)
- {
- backet++;
- sum += hlist[i].entry_count;
- if (hlist[i].entry_count > max_link)
- {
- max_link = hlist[i].entry_count;
- }
- if (hlist[i].entry_count > 1)
- {
- conflict_count++;
- }
- hit_count += hlist[i].hit_count;
- }
- }
- backet_usage = backet/1.0/backet_len * 100;;
- avg_link = sum/1.0/backet;
- printf("backet_len = %d/n", backet_len);
- printf("hash_call_count = %d/n", hash_call_count);
- printf("hit_count = %d/n", hit_count);
- printf("conflict count = %d/n", conflict_count);
- printf("longest hash entry = %d/n", max_link);
- printf("average hash entry length = %.2f/n", avg_link);
- printf("backet usage = %.2f%/n", backet_usage);
- }
- void usage()
- {
- printf("Usage: hashtest filename hash_func_name [backet_len]/n");
- printf("hash_func_name:/n");
- printf("/tsimple_hash/n");
- printf("/tRS_hash/n");
- printf("/tJS_hash/n");
- printf("/tPJW_hash/n");
- printf("/tELF_hash/n");
- printf("/tBKDR_hash/n");
- printf("/tSDBM_hash/n");
- printf("/tDJB_hash/n");
- printf("/tAP_hash/n");
- printf("/tCRC_hash/n");
- }
- void md5_to_32(unsigned char *md5_16, unsigned char *md5_32)
- {
- int i;
- for (i = 0; i < 16; ++i)
- {
- sprintf(md5_32 + i * 2, "%02x", md5_16[i]);
- }
- }
- int main(int argc, char *argv[])
- {
- int fd = -1, rwsize = 0;
- unsigned char md5_checksum[16 + 1] = {0};
- unsigned char buf[BLOCK_LEN] = {0};
- if (argc < 3)
- {
- usage();
- return -1;
- }
- if (-1 == (fd = open(argv[1], O_RDONLY)))
- {
- perror("open source file");
- return errno;
- }
- if (argc == 4)
- {
- backet_len = atoi(argv[3]);
- }
- hashtest_init();
- choose_hash_func(argv[2]);
- while (rwsize = read(fd, buf, BLOCK_LEN))
- {
- md5(buf, rwsize, md5_checksum);
- insert_hash_entry(md5_checksum, hlist);
- hash_call_count++;
- memset(buf, 0, BLOCK_LEN);
- memset(md5_checksum, 0, 16 + 1);
- }
- close(fd);
- show_hashtest_result();
- hashtest_clean();
- return 0;
- }
原文地址:
http://blog.csdn.net/liuben/article/details/5050697