Skip to content

Repository files navigation

HyperLogLog library for C language

Build status

See http://en.wikipedia.org/wiki/HyperLogLog

Example

/* * File: examples/rnd.c */#include<stdio.h>#include<stdlib.h>#include"../src/hll.h"intmain(intargc, char*argv[]) {
longi;
structHLLhll;
if(hll_init(&hll, 16) ==-1) {
perror("hll_init");
exit(1);
}
for(i=0; i<100000000; i++) {
longr=random() % 1000000;
hll_add(&hll, &r, sizeof(r));
}
printf("Estimate: %f\n", hll_count(&hll));
hll_destroy(&hll);
return0;
}
% cc -O2 -L lib -l hyperloglog examples/rnd.c -o examples/rnd
% time examples/rnd
Estimate: 994154.464585
examples/rnd 2,86s user 0,01s system 99% cpu 2,875 total

API

bitssize (bytes)standard error
41626.00%
53218.38%
66413.00%
71289.19%
82566.50%
95124.60%
1010243.25%
1120482.30%
1240961.62%
1381921.15%
14163840.81%
15327680.57%
16655360.41%
171310720.29%
182621440.20%
195242880.14%
2010485760.10%
structHLL {
uint8_tbits;
/* registers buffer */uint8_t*registers;
/* registers buffer size */size_tsize;
};

Initialize a storage

inthll_init(structHLL*hll, uint8_tbits);

Add key to set

voidhll_add(structHLL*hll, constvoid*buf, size_tsize);

Get estimated set size

doublehll_count(conststructHLL*hll);

Merge storages

inthll_merge(structHLL*dst, conststructHLL*src);

Cleanup

voidhll_destroy(structHLL*hll);

TODO

оптимизировать размер хранилища: при bits >= 4 && bits <= 16 достаточно 5 бит на регистр, вместо нынешних 8, получим экономию в 1.5+ раза. Для bits >= 17 && bits <= 20 достаточно всего 4 бит на регистр, то есть экономия в 2 раза.

bitsregs countbprbytessavingerror
41651037.50%26.00%
53252037.50%18.38%
66454037.50%13.00%
712858037.50%9.19%
8256516037.50%6.50%
9512532037.50%4.60%
101024564037.50%3.25%
1120485128037.50%2.30%
1240965256037.50%1.62%
1381925512037.50%1.15%
141638451024037.50%0.81%
153276852048037.50%0.57%
166553654096037.50%0.41%
1713107246553650.00%0.29%
18262144413107250.00%0.20%
19524288426214450.00%0.14%
201048576452428850.00%0.10%

About

HyperLogLog library for C programming language

Resources

Stars

8 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages