我正在尝试编写一个bloom filter,它可以存储大约80,000个strings...Now,我猜每个字符串可以有2个单词的长度。为了存储80,000个字符串..我需要80,000*2 = 16kBytes?
如果我必须存储16kB = 16*1000*8 =128,000位,我至少需要一个2^17=131,072位图。这就是我现在拥有的
int main() {
char *str = "hello world";
int c = sizeof(unsigned char);
/*
* declare the bit array
*/
unsigned char bit_arr[128000/c];
/*
* couple of hash functions
*/
unsigned int bkd = bkdrhash(str, strlen(str));
unsigned int rsh = rshash(str, strlen(str));
unsigned int jsh = jshash(str, strlen(str));
/*
* logic to set bit
* Access the bitmap arr element
* And then set the required bits in that element
*/
bit_arr[bkd/c] & (1 << (bkd%c));
bit_arr[rsh/c] & (1 << (rsh%c));
bit_arr[jsh/c] & (1 << (jsh %c));}
有没有更好的/最优的方法来做到这一点?
谢谢
发布于 2012-10-13 08:06:20
你的数学题错了。80k *2= 160K。尽管如此,正如克里斯·多德所说,在普通的台式机甚至智能手机上,这些都是相当小的。如果您的应用程序是嵌入式的,或者如果您有其他较大的分配,那么情况可能就不同了。默认情况下,iPhone的堆栈为1兆字节,辅助线程为1/2兆字节。
在总线宽度为N位的机器上,使用N位宽的整数可能具有显著的优势。因此,不要使用“大小”这个词:
#define WORD_BYTES 4
#define BYTE_BITS 8
#define WORD_BITS (BYTE_BITS * WORD_BYTES)
#define BITSET_BITS (1u << 17)
#define BITSET_WORDS (BITSET_BITS / WORD_BITS)
typedef unsigned int WORD;
typedef WORD BITSET[BITSET_WORDS];
typedef WORD *BITSET_REF;
#define bit(N) (1u << (N))
/* Allocate a bitset on the heap and return a reference to it. */
BITSET_REF new_bitset(void)
{
return safe_malloc(sizeof(BITSET));
}
/* Arrange for these functions to be inlined by the compiler rather
than using fancy macros or open coding. It will be better in
the long run. */
int is_set(BITSET_REF bitset, int n)
{
return (bitset[n / WORD_BITS] | bit(n % WORD_BITS)) != 0;
}
void set(BITSET_REF bitset, int n)
{
bitset[n / WORD_BITS] |= bit(n % WORD_BITS);
}
void clear(BITSET_REF bitset, int n)
{
bitset[n / WORD_BITS] &= ~bit(n % WORD_BITS);
}发布于 2012-10-13 07:46:01
除了各种明显的拼写错误之外,在堆栈上分配大型数组(作为局部变量)通常不是一个好主意。堆栈默认不是很大(通常只有8MB左右),虽然您可以重新配置以获得更大的堆栈,但通常在堆上分配大对象或使用静态分配要好得多。
也就是说,128K绝对不是“巨大的”。从很多方面来看,它甚至都不算“大”。关于它,你唯一能说的就是它不“小”。
https://stackoverflow.com/questions/12867496
复制相似问题