哈希表性能测试,为什么要用哈希表
简介
我就默认大家都清楚哈希表的原理、用途了。今天这篇文章侧重于讲解使用哈希表的必要性,并且用实验数据和自己的推理作为佐证。
哈希表性能测试
代码
最简单的一个哈希例子,记录了学生id和成绩的映射。没考虑异常处理,没考虑数据合理性。
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define HASH_TABLE_SIZE 520000000
typedef struct
{
int id;
int score;
} id_and_score;
typedef struct
{
id_and_score *val;
int count;
} HASH_TABLE;
int m = 0;
//初始化散列表
int init_hash_table(HASH_TABLE *H)
{
int i;
m = HASH_TABLE_SIZE;
H->count = m;
H->val = (id_and_score *)malloc(m * sizeof(id_and_score));
for (i = 0; i < m; i++)
{
H->val[i].id = i;
H->val[i].score = i+2;
}
return 1;
}
//散列函数
int hash_func(int key)
{
return key % m;
}
//散列表查找关键字
int search_by_hash(HASH_TABLE H, int key)
{
int addr = hash_func(key);
while